doumen5087 2017-01-31 21:34
浏览 52
已采纳

更新数据库时出现MySQL错误消息[重复]

This question already has an answer here:

The following code works just fine.

$q = "UPDATE openwhen SET o_year='$year', o_month='$month', o_date='$date', o_hour='$ohour', c_hour='$chour', combine='$combine' WHERE user_name='$un' LIMIT 1";

$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc));

However, when I add one more variable, as shown below, it fails with the indicated error message.

$q = "UPDATE openwhen SET o_year='$year', o_month='$month', o_date='$date', o_hour='$ohour', c_hour='$chour', combine='$combine', match='$mtch' WHERE user_name='$un' LIMIT 1";

$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc));

// An error occurred in script '/home/.../update.php' on line 96: Query: UPDATE openwhen SET o_year='2017', o_month='02', o_date='01', o_hour='15', c_hour='22', combine='2017-02-01', match='2' WHERE user_name='Charlie' LIMIT 1
// 
// MySQL Error: 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 'match='2' WHERE user_name='Charlie' LIMIT 1' at line 1

I had added the 'match' field to the database as a ten character VARCHAR, and thought that would work, but either there is something I'm missing in the php code, or something wrong in the database.

</div>
  • 写回答

1条回答 默认 最新

  • douzhanshen0657 2017-01-31 21:37
    关注

    match is a reserved keyword in mysql

    you should surround your field names with '`' to indicate that they are column names.

        UPDATE openwhen 
    SET    `o_year` = '$year', 
           `o_month` = '$month', 
           `o_date` = '$date', 
           `o_hour` = '$ohour', 
           `c_hour` = '$chour', 
           `combine` = '$combine', 
           `match` = '$mtch' 
    WHERE  `user_name` = '$un' 
    LIMIT 1 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度