更新数据库时出现MySQL错误消息

时间:2017-01-31 21:34:48

标签: php mysql sql-update

以下代码可以正常使用。

$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\n<br />MySQL Error: " . mysqli_error($dbc));

但是,当我再添加一个变量时,如下所示,它会因指示的错误消息而失败。

$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\n<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

我添加了&#39;匹配&#39;将数据库字段作为十个字符的VARCHAR,并认为可行,但要么在PHP代码中缺少某些内容,要么在数据库中出现错误。

1 个答案:

答案 0 :(得分:3)

match是mysql中的保留关键字

你应该用&#39;`&#39;来围绕你的字段名称。表明它们是列名。

    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