Jump to content

CSV to MySql Upload Infinite Loop


rkellermeyer

Recommended Posts

Hey everyone ... This should be a pretty easy question for someone who is used to using fgetcsv:

 

I have a script that needs to import a csv file into a mysql table ... I keep getting stuck in a loop that only ends when the script hits the 30 second timeout I have set on the server ... It never actually inserts a record, and it never spits out a message indicating and error ... Can someone help figure out why I get caught in this loop?

 

Here's the code:

function import_csv()
{
if(isset($_POST['submit_csv']))
   {
	 $filename=$_POST['filename'];
	 $handle = fopen("$filename", "r");
	 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
	 {
	   $import="INSERT INTO `{mydatabase}`.`{my table}` (`name`, `qrl`, `begin`) VALUES ('$data[0]','$data[1]','$data[2]')";
	   mysql_query($import) or die(mysql_error());
	 }
	 fclose($handle);
	 print "Import done";
    }
   else
   {
	  print "<form action='results.php' method='post'><input type='file' name='filename'><br /><input type='submit' value='submit' name='submit_csv' /></form>";
   }
}

Link to comment
https://forums.phpfreaks.com/topic/233132-csv-to-mysql-upload-infinite-loop/
Share on other sites

This code is not doing what you think it's doing. I'm pretty sure $_POST['filename'] will be blank.

Instead, what you want is to use $_FILES[]

	if(isset($_POST['submit_csv']) && $_FILES['filename']['error'] == UPLOAD_ERR_OK)
{
	$filename = $_FILES['filename']['tmp_name'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.