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