jrobles Posted November 10, 2009 Share Posted November 10, 2009 I have a script that imports the data from a csv file. before I insert into the table I want to log the import i.e. row count, import date, file name...etc. I got most of the the functions working, I am just having trouble getting the record count of the csv file. $link = connectToDB(); if(isset($_POST['submit'])) {$filename=$_POST['filename']; $handle = fopen("$filename", "r"); //LOG THE BATCH IMPORT $user=$_SESSION['SESS_MEMBER_ID']; $log_query="INSERT INTO batchImportLog(ImportDate,FileLocation,RecordCount, ImUserID ) VALUES(NOW(),'xxx','xxx','$user')"; mysql_query($log_query) or die(mysql_error()); //GRAB LAST ID FROM LOG TABLE $batchid=mysql_insert_id(); //INSERT EACH ROW INTO THE TABLE while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {$import="INSERT INTO inventoryTable(campcode,imbatchid, importdate,fname, mname, lname, phone, address1, address2, city, state, zip) VALUES('$data[0]','$batchid',NOW(),'$data[1]','$data[2]','$data[3]', '$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')"; mysql_query($import) or die(mysql_error());} fclose($handle); print "Import done";} else {print "<form action='import.php' method='post'>"; print "Type file name to import:<br>"; print "<input type='text' name='filename' size='20'><br>"; print "<input type='submit' name='submit' value='submit'></form>";} Quote Link to comment https://forums.phpfreaks.com/topic/181029-import-csv-into-mysql-table-and-log-record-count-fopen/ Share on other sites More sharing options...
dreamwest Posted November 10, 2009 Share Posted November 10, 2009 $link = connectToDB(); if(isset($_POST['submit'])){ $filename=$_POST['filename']; $handle = fopen("$filename", "r"); //GRAB LAST ID FROM LOG TABLE $batchid=mysql_insert_id(); //INSERT EACH ROW INTO THE TABLE $cnt = 1; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){ $import="INSERT INTO inventoryTable(campcode,imbatchid, importdate,fname, mname, lname, phone, address1, address2, city, state, zip) VALUES('$data[0]','$batchid',NOW(),'$data[1]','$data[2]','$data[3]', '$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')"; mysql_query($import) or die(mysql_error()); ++$cnt; } fclose($handle); print "Import done"; //LOG THE BATCH IMPORT $user=$_SESSION['SESS_MEMBER_ID']; $log_query="INSERT INTO batchImportLog(ImportDate,FileLocation,RecordCount, ImUserID ) VALUES(NOW(),'xxx','{$cnt}','{$user}')"; mysql_query($log_query) or die(mysql_error()); print '<br>All done!'; }else{ print "<form action='import.php' method='post'>"; print "Type file name to import:<br>"; print "<input type='text' name='filename' size='20'><br>"; print "<input type='submit' name='submit' value='submit'></form>"; } Quote Link to comment https://forums.phpfreaks.com/topic/181029-import-csv-into-mysql-table-and-log-record-count-fopen/#findComment-955167 Share on other sites More sharing options...
jrobles Posted November 12, 2009 Author Share Posted November 12, 2009 worked like a champ, thanks a million Quote Link to comment https://forums.phpfreaks.com/topic/181029-import-csv-into-mysql-table-and-log-record-count-fopen/#findComment-955912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.