-3

For example I have a table with an id(primary key) and a name. User can either add a new row or update an existing row. One way is I can check whether the id is posted back, then I will update, otherwise it is a new record and I will insert. Is there a way to do it in one sql statement, like the ON DUPLICATE KEY UPDATE. Thanks.

Note: The reason I ask this is the primary key is auto-increment and it will be null when I retrieve it from the $_POST;

leon
  • 435
  • 1
  • 4
  • 12

1 Answers1

1

You may try like this:

INSERT INTO table (id,a,b,c,d)
VALUES (1,2,3,4,5) 
ON DUPLICATE KEY
    UPDATE a=a, b=b, c=c, d=d;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331