0

How to write PHP Function

If row existed in db query should be Update else insert query

Srinivas P
  • 28
  • 6

1 Answers1

3

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

Check out MySQL INSERT ... ON DUPLICATE KEY

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:

Example:

INSERT INTO table (id, name, age) VALUES(1, "A", 19) ON DUPLICATE KEY UPDATE    
name="A", age=19
Daniel
  • 10,641
  • 12
  • 47
  • 85