$sql = "INSERT INTO couponentries (itemid, coupon, MSISDN, channel, result)
VALUES ('".$itemid."','".$CouponCode."', '".$MSISDN."','".$channel."','".$status."')
ON DUPLICATE KEY UPDATE couponentries.result = VALUES('Invalid couponcode[ERR: Already exists]')";
我正在尝试将新项目从 PHP 网络表单插入到 MySQL 数据库。如果我插入重复的行,我会将结果更新为一条错误消息。这是我的代码。它一直给我一个语法错误。
ERROR: Could not able to execute 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 ''Invalid couponcode[ERR: Already exists]')' at line 3
最佳答案
您正在使用字符串文字来更新您的列 - 丢失值
:
$sql = "INSERT INTO couponentries (itemid, coupon, MSISDN, channel, result)
VALUES ('".$itemid."','".$CouponCode."', '".$MSISDN."','".$channel."','".$status."')
ON DUPLICATE KEY UPDATE couponentries.result = 'Invalid couponcode[ERR: Already exists]'";
强制性旁注:
像这样连接字符串会使您的代码容易受到 SQL 注入(inject)攻击。你应该看看 Prepared Statements相反。
关于php - 插入MySQL "on duplicate key update"语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50763965/