Note that there are some explanatory texts on larger screens.

plurals
  1. POImporting from CSV to MySQL using Update
    primarykey
    data
    text
    <p>I have this php code that is aimed for importing all rows and columns in csv file into mysql table as records. It successfuly inserts from csv into mysql table called "import_items" <em>BUT</em> if i re-do the import, it will insert duplicate records. All i need is where to fit the Query Update in which it should check <strong>IF ITEM EXISTS in table name</strong>-> Update values , <strong>IF NOT EXISTS</strong>..Insert record!</p> <p>Here is my code :</p> <pre><code>&lt;form enctype="multipart/form-data" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="post"&gt; File to import:&lt;br /&gt; &lt;input size='30' type='file' name='filename'&gt; &lt;input type="submit" name="submit" value="Upload"&gt;&lt;/form&gt; &lt;?php require_once('connect_db.php'); //Upload File if (isset($_POST['submit'])) { if (is_uploaded_file($_FILES['filename']['tmp_name'])) { //Import uploaded file to Database $row = 1; $handle = fopen($_FILES['filename']['tmp_name'], "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { //Update Database Values $import="insert into import_items (item_no,qty,actual_price,discount_price,difference_price,date) VALUES('".mysql_real_escape_string($data[0])."', '".mysql_real_escape_string($data[1])."', '".mysql_real_escape_string($data[2])."', '".mysql_real_escape_string($data[3])."', '".mysql_real_escape_string($data[4])."', '".$date = date('Y-m-d')."')"; mysql_query($import) or die(mysql_error()); } fclose($handle); } } ?&gt; </code></pre> <p>Thank You</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload