Jump to content

Updating Multiple Rows in MySQL with a .CSV File Import - HELP!


brainstorm

Recommended Posts

Hi all,

 

So far, I've gotten the following code to work just fine to INSERT new data into the DB. However, when I change things up a bit by adding the "UPDATE" function instead of the "INSERT" function, it gives me an error.

 

Any Help is much appreciated! I'm trying to update the same clients with new info. Here's what I have so far...

 

Code for file upload to directory on server:

 

<?php if ($_REQUEST[completed] != 1) { ?>
			<b>Please upload Your .CSV file to update the Database.</b><br>
			<form enctype=multipart/form-data method=post>
			<input type=hidden name=MAX_FILE_SIZE value=1500000>
			<input type=hidden name=completed value=1>
			Choose file to send: <input type=file name=mailfile> and
			<input type=submit></form>
			<?php } else { ?>
			<br /><br /><b>Excel file successfully uploaded!</b>
	<?php } ?>

Script to insert the data once the file has been uploaded:

 

<?
include 'dbc.php';

   /// first get a mysql connection as per the FAQ

  $fcontents = file ('users.csv');
  /// expects the csv file to be in the same dir as this script

  for($i=0; $i<sizeof($fcontents); $i++) {
      $line = trim($fcontents[$i]);
      $arr = explode(',', $line);
      ///if your data is comma separated
      /// instead of tab separated,
      /// change the '\t' above to ','

      $sql = "insert into users values ('".
                  implode("','", $arr) ."')";
      mysql_query($sql);
      echo $sql ."<br>\n";
      if(mysql_error()) {
         echo mysql_error() ."<br>\n";
      }
}
?>

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.