0
while (($test= fgetcsv($file, 10000, ",")) !== FALSE){
    $field1= $test[0]; 
    $field2= $test[1];
    $sql= "INSERT IGNORE into tablename(field1, field2) values('$field1','$field2')"; 
    mysql_query($sql);
}

I am importing data from a CSV file and then store this data into variables $field1 , $field2 then I try to insert into tablename table. IN CSV file i have many records. My query insert the data but its inserting duplicate records i use IGNORE in query but its not working for me. How Can i prevent duplication?

2 Answers 2

1

You need to have a primary key or a unique key in your table. Please refer to the manual.

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

2 Comments

My Table first field is AUTO INCREMENTED with UNSIGNED attribute tableID
Please post the result of SHOW CREATE TABLE your_table_name;
0

If all you want is to combine duplicated rows, use SELECT DISTINCT.

Comments

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.