samtwilliams Posted October 11, 2009 Share Posted October 11, 2009 Evening All, I am currently building a script that will upload a csv file and then parse it to the database. I have got the file to upload and read but can't work out why its not writing it to the database. My error message reads FAIL which is telling me its not parsing the file. Can anyone spot any issues in my code? <?PHP if (isset($_POST['upload_mass'])) { $uploadname = basename( $_FILES['uploaded']['name']); if ($uploadname == '') { $warning = 'You did not select a .csv file'; } else { $target = "tmp/"; $target = $target . basename( $_FILES['uploaded']['name']); move_uploaded_file($_FILES['uploaded']['tmp_name'], $target); $output = array('Pass' => 0, 'Fail' => 0); ini_set('auto_detect_line_endings',1); $handle = fopen("tmp/{$uploadname}", 'r'); while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) { $val1 = mysql_real_escape_string($data[0]); $val2 = mysql_real_escape_string($data[1]); $val3 = mysql_real_escape_string($data[2]); mysql_query("INSERT INTO `teeress` (`id`,`name`,`date`,`further_info`) VALUES ('','{$val1}','{$val2}','{$val3}')"); $result = (mysql_insert_id()> 0) ? 'Pass' : 'Fail' ; $output[$result]++; } print_r($result); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/177342-csv-file-to-database/ Share on other sites More sharing options...
ozestretch Posted October 12, 2009 Share Posted October 12, 2009 have you printed the query itself? if so, have you ran that query directly into the database? phpmyadmin etc.. Quote Link to comment https://forums.phpfreaks.com/topic/177342-csv-file-to-database/#findComment-935350 Share on other sites More sharing options...
PFMaBiSmAd Posted October 12, 2009 Share Posted October 12, 2009 The posted code does not contain any code to connect to a database server or select a database once you are connected. Are you developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php would help you by displaying all the errors it detects? Stop and start your web server to get any change made to php.ini to take effect. Also, for debugging, echo mysql_error() on the line immediately after your mysql_query() statement to find out why a specific query failed to execute. Quote Link to comment https://forums.phpfreaks.com/topic/177342-csv-file-to-database/#findComment-935383 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.