2

I have a PHP script that imports a csv into a MySQL database. The import works fine using this line of code

$query = "insert into $databasetable values('$linemysql');";

The only problem is when I try to import another csv with duplicates the script errors out. How do I adjust this script to skip duplicates if they exist?

2 Answers 2

4

INSERT IGNORE won't insert a row if that would result in a duplicate key. However, it will also ignore (and not generate an error) some other cases like trying to insert NULL into a column with NOT NULL constraint.

If you just want to ignore duplicates but still get other insert errors, use INSERT ... ON DUPLICATE KEY UPDATE instead. This will update rows with duplicating keys, although comes with some performance cost.

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

1 Comment

didn't know about those keywords +1 :)
2

if you are using mysql try:

$query = "insert ignore into $databasetable values('$linemysql');";

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.