PHP MYSQL UPDATE if Exist or INSERT if not?

Two choices:

MySQL Manual :: INSERT INTO ... ON DUPLICATE KEY UPDATE Syntax

or

MySQL Manual :: REPLACE INTO Syntax

Both will allow you to do the insert-if-not-exists-or-update in one query.


I believe you are looking for the following syntax:

INSERT INTO <table> (field1, field2, field3, ...) 
VALUES ('value1', 'value2','value3', ...)
ON DUPLICATE KEY UPDATE
field1='value1', field2='value2', field3='value3', ...

Note: With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values.

MySQL Documentation: INSERT ... ON DUPLICATE KEY UPDATE Statement