开发者

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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜