Of course, you can use INSERT IGNORE INTO, for example:
INSERT IGNORE INTO beer(name, type, alcohol_by_volume, description, image_url) VALUES('{$name}', {$type}, '{$alcohol_by_volume}', '{$description}', '{$image_url}')
You can also use ON DUPLICATE KEY , but if you just don't want to add the INSERT IGNORE INTO , this is the best choice. ON DUPLICATE KEY better suited if you want to do something more specific when there is a duplicate.
If you decide to use ON DUPLICATE KEY , avoid using this clause in tables with several unique indexes. If you have a table with several unique ON DUPLICATE KEY -clause may give unexpected results (you really don't have 100% control over what happens)
Example: - this row below updates only one row (if the type is 1 and alcohol_by_volume 1 (and both columns are unique indexes))
ON DUPLICATE KEY UPDATE beer SET type=3 WHERE type=1 or alcohol_by_volume=1
Summarizing:
ON DUPLICATE KEY just does the job without warning or error if there are duplicates.
INSERT IGNORE INTO gives a warning when there are duplicates, but other than that it is simply ignored to insert the duplicate into the database.
source share