update record if exists, else write new record?
I'm trying to execute this query and when it finds a record for $serial, it can update it. OR, if the serial doesn't exist, it can write a开发者_运维问答 new record.
I get syntax erros but to me it seems fine. pretty darn sure ON DUPLICATE KEY UPDATE is the way to go but i'm missing something...
$query = "INSERT INTO `".$sys_id."` (serial, status) VALUES ('98745', 'active') ON DUPLICATE KEY UPDATE";
$result = mysql_query($query) or die(mysql_error());
you need to tell it what to update (see manual)
so:
$query = "INSERT INTO `".$sys_id."` (serial, status) VALUES ('98745', 'active')
ON DUPLICATE KEY UPDATE status=VALUES(active)";
(i am under the assumption that the key is serial
)
精彩评论