Insert new data on duplicate update
I need help in writing a php mysql query that insert data to a table but when it come to the ID(primary key), the query should update the other fields.
I have tried the following:
mysql_query("INSERT INTO TableName(ID,Field1,Field2) V开发者_如何转开发ALUES('$ID','$F1','$F2') ON DUPLICATE KEY UPDATE SET Field1='$F1',Field2;",$conServer) or die(mysql_error());
I think you should try REPLACE instead of INSERT: "REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted"
See MySQL reference: http://dev.mysql.com/doc/refman/5.0/en/replace.html
i did this:(if I understood your what you are asking...
INSERT INTO YOUR_TABLE (FIELD1,FIELD2, FIELD3) VALUES ('value_FIELD1',value_FIELD2, value_FIELD3)
ON DUPLICATE KEY UPDATE FIELD4 = value_FIELD4,FIELD5 = value_FIELD5";
精彩评论