Import data from csv file to mysql
Posted: Wed Oct 07, 2009 4:27 am
Hi techies,
As i am working on file upload concept in php. When i browse the csv file from any directory to upload data to mysql i am getting the following error. Apart from getting errors the empty records are added to the mysql database and also the script will continue executing.(which goes to the infinite loop)
The errors as follows:
Code:
Cheers....
phphunger
As i am working on file upload concept in php. When i browse the csv file from any directory to upload data to mysql i am getting the following error. Apart from getting errors the empty records are added to the mysql database and also the script will continue executing.(which goes to the infinite loop)
The errors as follows:
Code: Select all
Warning: fopen() [function.fopen]: Filename cannot be empty in C:\xampp\htdocs\upload\import1.php on line 11
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\upload\import1.php on line 13
Code: Select all
<?php
include "connect.php";
if(isset($_POST['submit']))
{
$filename=$_FILES['filename'];
$handle = fopen("$filename", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$import="INSERT into info(name,address,food) values('$data[0]','$data[1]','$data[2]')";
mysql_query($import) or die(mysql_error());
}
fclose($handle);
print "Import done";
}
else
{
print "<form action='import1.php' method='post'>";
print "Browse file to import:</br>";
print "<input type='file' name='filename'></br>";
print "<input type='submit' name='submit' value='submit'></form>";
}
?>phphunger