douba9425 2015-12-01 16:31 采纳率: 100%
浏览 48
已采纳

关于重复键错误

I'm trying to check to see if there is a duplicate record and then either insert or update. I have made to_number field (which is a phone number) a UNIQUE field. There is a PRIMARY KEY "id" that is auto-increment. Not sure if this is what's causing the headache. Here is my code and then below that the error.

$sql = "INSERT INTO DC**** (id, dcsrep, name, to_number, amount, date, digits, details)
VALUES ('', '$dcsrep', '$name', '$to_number', '$amount', '$date', '$digits', '$details') 
ON DUPLICATE KEY UPDATE dcsrep = values($dcsrep), name = values($name), to_number = values($to_number), amount = values($amount), date = values($date), digits = values($digits), details = values($details)";

Error: INSERT INTO ***Auth (id, dcsrep, name, to_number, amount, date, digits, details) VALUES ('', 'notreal@notreal.com', 'Test Johnson', '+15555551212', '150.00', '2015-12-16', '1234', 'Testing again.') ON DUPLICATE KEY UPDATE dcsrep = values(notreal@notreal.com), name = values(Test Johnson), to_number = values(+15555551212), amount = values(150.00), date = values(2015-12-16), digits = values(1234), details = values(Testing again.) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@notreal.com), name = values(Test Johnson), to_number = values(+15555551212),' at line 2

  • 写回答

1条回答 默认 最新

  • dqhbuwrwq692118284 2015-12-01 17:42
    关注

    From the documentation:

    You can use the VALUES(col_name) function in the UPDATE clause to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE statement.

    But you are not providing the column names, you are providing the values again.

    So, either you provide the column name again:

    ON DUPLICATE KEY UPDATE dcsrep = values(dcsrep), ...
    

    or you skip the VALUES() function and provide the value:

    ON DUPLICATE KEY UPDATE dcsrep='$dcsrep', ...
    

    Note:

    There is no reason to update all columns when you only want to update the phone number. If all other fields stay the same anyway, just update the column that holds the phone number.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀