Jump to content

Need some help with importing a CSV file.


JSHINER

Recommended Posts

<?php

$handle = fopen ( ' file.csv', 'r');

if (!$handle) {

echo 'failed to open file';

exit();

}

while ($row = fgetcsv($handle, 999)) {
					  
$db->query('INSERT INTO user (email, password, first_name, last_name, address, city, state, zipcode, company, phone) 
			VALUES (\'' . $db->escape(trim($row[0])) . '\', 
					\'' . $db->escape(trim($row[1])) . '\',
					\'' . $db->escape(trim($row[2])) . '\',
					\'' . $db->escape(trim($row[3])) . '\',
					\'' . $db->escape(trim($row[5])) . '\',
					\'' . $db->escape(trim($row[6])) . '\',
					\'' . $db->escape(trim($row[7])) . '\',
					\'' . $db->escape(trim($row[8])) . '\',
					\'' . $db->escape(trim($row[4])) . '\',
					\'' . $db->escape(trim($row[9])) . 
					'\')');

}
?>

 

The above code works but kicks an error back if there's a duplicate entry and doesn't complete the task. How can I get it to check for the email before inserting ? 

Link to comment
https://forums.phpfreaks.com/topic/94253-need-some-help-with-importing-a-csv-file/
Share on other sites

Wrap your insert in a try/catch.  You can then control the behavior of the script.

 

http://uk2.php.net/exceptions

 

while ($row = fgetcsv($handle, 999)) {
  try {						  
    $db->query(...);
  }
  catch (Exception $e) {
    // silently skip the rest of the loop and continue to the next record
    continue;
  }
} 

while ($row = fgetcsv($handle, 999)) {
  try {						  
    $db->query('SELECT email FROM user WHERE email = ' . $db->escape(trim($row[0])) . '');
  }
  catch (Exception $e) {
    // silently skip the rest of the loop and continue to the next record
    continue;
					  
$db->query('INSERT INTO user (email, password, first_name, last_name, address, city, state, zipcode, company, phone) 
			VALUES (\'' . $db->escape(trim($row[0])) . '\', 
					\'' . $db->escape(trim($row[1])) . '\',
					\'' . $db->escape(trim($row[2])) . '\',
					\'' . $db->escape(trim($row[3])) . '\',
					\'' . $db->escape(trim($row[5])) . '\',
					\'' . $db->escape(trim($row[6])) . '\',
					\'' . $db->escape(trim($row[7])) . '\',
					\'' . $db->escape(trim($row[8])) . '\',
					\'' . $db->escape(trim($row[4])) . '\',
					\'' . $db->escape(trim($row[9])) . 
					'\')');

}

  }

 

I get the error: "

Fatal error: Error in database query. Query was [sELECT email FROM user WHERE email = [email protected]] and the error returned was [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@email.com' at line 1] in . . ."

You are incorrectly quoting the string.

 

Try:

<?php
  try {						  
    $db->query("SELECT email FROM user WHERE email = '" . $db->escape(trim($row[0])) . "'");
  }
?>

 

Also, I would use an array of fieldnames to make creating the insert query easier. My method uses the alternative insert syntax:

<?php
$flds = array('email', 'password', 'first_name', 'last_name', 'address', 'city', 'state', 'zipcode', 'company', 'phone');
while ($row = fgetcsv($handle, 999)) {
  try {						  
    $db->query("SELECT email FROM user WHERE email = '" . $db->escape(trim($row[0])) . "'");
  }
  catch (Exception $e) {
      $qtmp = array();
      for($i = 0; $i<count($flds);$i++);
          $qtmp[] = $flds[$i] . " = '" . $db->escape(trim($row[$i])) . "'";
      $q = "insert into user set " . implode(', ',$qtmp);
      $db->query($q);
   }
}
?>

 

Ken

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.