0

How to write PHP Function

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

0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.