Rectangle 27 0

php Database : if exists 'update' if not exists insert?


INSERT INTO table (id, name, age) VALUES(1, "A", 19) ON DUPLICATE KEY UPDATE    
name="A", age=19

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:

You don't have to use php to achieve that. You can do it with pure SQL syntax using the ON DUPLICATE KEY:

Note
Rectangle 27 0

php Database : if exists 'update' if not exists insert?


INSERT INTO table (id, name, age) VALUES(1, "A", 19) ON DUPLICATE KEY UPDATE    
name="A", age=19

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:

You don't have to use php to achieve that. You can do it with pure SQL syntax using the ON DUPLICATE KEY:

Note