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
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'];

Link to comment
Share on other sites

Facepalm. You need to add enctype='multipart/form-data' to the form.

		  print "<form action='results.php' enctype='multipart/form-data' method='post'><input type='file' name='filename'><br /><input type='submit' value='submit' name='submit_csv' /></form>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.