Inserting into MySQL UNIQUE indexes can lead to errors (e.g. Duplicate entry ‘3’ for key 2). Either use INSERT IGNORE or the ON DUPLICATE KEY UPDATE Syntax.
13.2.5.3 INSERT … ON DUPLICATE KEY UPDATE Syntax If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL performs an UPDATE of the old row. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have similar effect:
Source: MySQL :: MySQL 5.7 Reference Manual :: 13.2.5.3 INSERT … ON DUPLICATE KEY UPDATE Syntax