0

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" BUT 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 IF ITEM EXISTS in table name-> Update values , IF NOT EXISTS..Insert record!

Here is my code :

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

File to import:<br />

<input size='30' type='file' name='filename'>

<input type="submit" name="submit" value="Upload"></form>



<?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); 
     }
} 
    ?>

Thank You

3 Answers 3

2

Check out the INSERT INTO ... ON DUPLICATE KEY UPDATE ... syntax. Oh, and make sure you have a primary key in your table, it's almost always a good idea.

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

Comments

0

MySQL has a statement which will check for an existing row and only insert if it doesn't exist. Use

REPLACE INTO import_items...

instead of INSERT INTO. The syntax is otherwise identical.

This statement will check for duplicates by the primary key of the table. If the key doesn't exist, it'll insert a row. If the key exists, it will update the other fields in the row. It's equivalent to you doing a SELECT first, then branching to do an UPDATE or INSERT.

3 Comments

REPLACE usually overwrites via primary key, rather than inserting only when the primary key doesn't exist...
@Matt S , I've tried REPLACE INTO and I'm still having Duplicates :(
@Alihamra, whichever solution you go with, make sure you have a primary key on the table to avoid duplicates.
0

You need to share your table schema. Using REPLACE INTO or INSERT ... ON DUPLICATE KEY UPDATE will not work if you don't have proper keys across the columns for which you want to enforce no duplicates. The other thing that is easy to do (one you have de-duped the table), is place a unique key on the column(s) where you want to prevent duplicates.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.