开发者

Why doesn't this statement update the data in MySQL?

开发者 https://www.devze.com 2023-03-11 04:57 出处:网络
I\'m new to MySQL. Whats wrong with this code? It doesn\'t update data. \"INSERT INTO highscores (name, score, maila, ip)\" . \"VALUES (\'$name\', \'$score\', \'$maila\', \'$ip\')\" .

I'm new to MySQL. Whats wrong with this code? It doesn't update data.

"INSERT INTO highscores (name, score, maila, ip)" . "VALUES ('$name', '$score', '$maila', '$ip')" .
"ON DUPLICATE KEY UPDATE score;" . "UPDATE highscores SET (if score>'$score') {score=$score} WHERE name=$name"

This works:

"INSERT INTO highscores (name, score, maila, ip) ".
     "VALUES ('$name', '$score', '$maila', '$ip') " . "
on duplicate key update score = greatest开发者_开发技巧(score, $score)"

Thanks to binaryLV: MSQL: How to overwrite entry only if new one is higher? else create new entry


When you use ON DUPLICATE KEY UPDATE, you must also specify what to update, that is, in PHP, the right query would be:

$q = "INSERT INTO highscores (name, score, maila, ip) ".
     "VALUES ('$name', '$score', '$maila', '$ip') ".
     "ON DUPLICATE KEY UPDATE score='$score'";
0

精彩评论

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