Jump to content

csv file to database


samtwilliams

Recommended Posts

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);
}

}
?>

Link to comment
https://forums.phpfreaks.com/topic/177342-csv-file-to-database/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/177342-csv-file-to-database/#findComment-935383
Share on other sites

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.