rkellermeyer Posted April 8, 2011 Share Posted April 8, 2011 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 More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 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 https://forums.phpfreaks.com/topic/233132-csv-to-mysql-upload-infinite-loop/#findComment-1198961 Share on other sites More sharing options...
rkellermeyer Posted April 8, 2011 Author Share Posted April 8, 2011 Still getting the loop ... rtfm now ... again. Link to comment https://forums.phpfreaks.com/topic/233132-csv-to-mysql-upload-infinite-loop/#findComment-1198970 Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 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 https://forums.phpfreaks.com/topic/233132-csv-to-mysql-upload-infinite-loop/#findComment-1198983 Share on other sites More sharing options...
rkellermeyer Posted April 8, 2011 Author Share Posted April 8, 2011 Yeah, just slapped myself ... Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/233132-csv-to-mysql-upload-infinite-loop/#findComment-1198989 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.