Im trying to import a .CVS file with First name and last name. But what it only does is import it to just one column in my database. And i get Notice: Undefined offset: 1 in ..insertimportres.php on line 91
and that is $enamn = $filesop[1];
that is complains on.
CVS/Excel
PHP
<?php
include ("connection.php");
if(isset($_POST["submit"]))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$fnamn = $filesop[0];
$enamn = $filesop[1];
$sql = mysql_query("INSERT INTO kandidater (fnamn, enamn) VALUES ('$fnamn','$enamn')");
$c = $c + 1;
}
if($sql){
echo "You database has imported successfully";
}else{
echo "Sorry! There is some problem.";
}
}
</div>
Basically the error means that it's trying to get the value at index 1 but the index doesn't exist. It means that somewhere in your csv there is no value for the second column. To prevent such errors assign the variables like this:
$fnamn = isset($filesop[0]) ? $filesop[0] : null;
$enamn = isset($filesop[1]) ? $filesop[1] : null;
If the index is set then it uses the value from that, if not it sets the value to null. Depending on your DB col definitions (if you have not null) this might fail also. In either case you can change null to whatever you need