0

Hi Ive been playing around with following code to import a csv file. How do I put it in such away that it

  1. Checks if a record already exists.
  2. Update a Record if the need be.
  3. Create a New Record if it doesn't exist.
  4. Create a new csv file with information on any extra records or updates done. Thanks

       <?php
    
      $connect = mysql_connect('localhost','root','12345');
      if (!$connect) {
      die('Could not connect to MySQL: ' . mysql_error());
       }
    
       $cid =mysql_select_db('test',$connect);
      // supply your database name
    
       define('CSV_PATH','C:/wamp/www/csvfile/');
      // path where your CSV file is located
    
      $csv_file = CSV_PATH . "infotuts.csv"; // Name of your CSV file
       $csvfile = fopen($csv_file, 'r');
       $theData = fgets($csvfile);
       $i = 0;
       while (!feof($csvfile)) {
       $csv_data[] = fgets($csvfile, 1024);
      $csv_array = explode(",", $csv_data[$i]);
      $insert_csv = array();
      $insert_csv['ID'] = $csv_array[0];
      $insert_csv['name'] = $csv_array[1];
      $insert_csv['email'] = $csv_array[2];
       $query = "INSERT INTO csvdata(ID,name,email)
       VALUES('','".$insert_csv['name']."','".$insert_csv['email']."')";
      $n=mysql_query($query, $connect );
      $i++;
       }
       fclose($csvfile);
    
       echo "File data successfully imported to database!!";
       mysql_close($connect);
       ?>
    
1
  • You have it right in your question. First, check to see if the record exists. If it doesn't insert it. If it does, check to see if needs to be updated. If it does, update it. Commented Jun 30, 2015 at 14:24

1 Answer 1

1

For your questions 1 to 3: use an "upsert", in MySQL:

$query = "INSERT INTO csvdata (id,name,email) 
  VALUES ('".$insert_csv['ID']."','".$insert_csv['name']."','".$insert_csv['email']."')
  ON DUPLICATE KEY UPDATE name='".$insert_csv['name']."', email='".$insert_csv['email']."';";

This will insert the recored in the database, unless a key is duplicated (in this case, i presume your primary key is "ID). http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html

for your question no 4, use mysql_affected_rows (http://php.net/manual/en/function.mysql-affected-rows.php)

if it return a value higher than 0, something was inserted/updated. Then you could write the value in a separate array and write it to a new CSV at the end.

$fh = fopen('altered_rows.csv', 'w');
[...]
$n=mysql_query($query, $connect );
$affected_rows = mysql_affected_rows($connect);
if($affected_rows>0) fputcsv($fh, $insert_csv);
[...]
fclose($fh);

http://php.net/manual/en/function.fputcsv.php

Sign up to request clarification or add additional context in comments.

1 Comment

also, try to use the PDO extension or mysqli. the mysql lib you are using is deprecated in php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.