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?
INSERT IGNOREwon’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 insertNULLinto a column withNOT NULLconstraint.If you just want to ignore duplicates but still get other insert errors, use
INSERT ... ON DUPLICATE KEY UPDATEinstead. This will update rows with duplicating keys, although comes with some performance cost.