我只是在修改一个简单的时间表脚本,如果我在数据库中做一个简单的插入,一切都很好。我也可以更新条目没问题,我的问题是我想更新并保留信息(这在我使用的 CONCAT 上运行良好)但是一旦我使用 ON DUPLICATE KEY UPDATE 它就会中断。
$sql="INSERT INTO sheet(employee, workdate, location, description, timein, timeout, timespent)
VALUES('$employee', CURDATE(), '$location', '$description', '$timein', '$timeout', '$timespent')";
//ON DUPLICATE KEY UPDATE
//location=VALUES(location),
//description=VALUES(description),
//timein=VALUES(timein),
//timeout=VALUES(timeout),
//timespent=VALUES(timespent)
//WHERE employee=$employee";
echo $sql;echo "<br>";
mysql_query($sql)or die(mysql_error());
如果我取消注释重复键,我会收到“您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 9 行的 'WHERE employee='Eric'' 附近使用”错误。我能想到的唯一一件事是我在一张完全空的桌子上做这件事,但我认为 INSERT 会解决这个问题。我所有的 $variables 都是干净的(没有 PDO,也必须学习)只是 real_escape_string
$sql="INSERT INTO sheet(employee, workdate, location, description, timein, timeout, timespent)
VALUES('$employee', CURDATE(), '$location', '$description', '$timein', '$timeout', '$timespent')
ON DUPLICATE KEY UPDATE
location=VALUES(location),
description=VALUES(description),
timein=VALUES(timein),
timeout=VALUES(timeout),
timespent=VALUES(timespent)
WHERE employee='$employee'";
echo $sql;echo "<br>";
mysql_query($sql)or die(mysql_error());