【问题标题】:MySQL Errno 1064MySQL 错误 1064
【发布时间】:2015-07-03 09:31:16
【问题描述】:

我有这个php 脚本,它使用三个变量$field、$value 和$id 更新我想要的字段:

<?php

include_once("DBconnect.php");

$field = $_POST["field"];
$value = $_POST["value"];
$id = $_POST["id"];

if(mysqli_query($link," UPDATE users SET $field='$value' WHERE id='$id' ")) {
    echo "INFO1";
}
else {
    echo mysqli_errno($link)." ".mysqli_error($link);
}
?>

我使用这个 AJAX 脚本来发送三个变量:

$(document).on("change",".UserEdit_group",function(){

    clearTimeout(TimerVar);
    var UserEditedId = $(this).parents(".tmptr").prev("tr").find(".CPidCell").text().trim();
    var UserEditedLog = $(this).parents(".tmptr").find(".UserEditLog");

    alert (UserEditedId);

    UserEditedLog.html("<img src='Resources/Images/Loader02.gif'/>");
    UserEditedLog.css({"overflow":"hidden"});
    UserEditedLog.animate({"max-height" : "1000px"},1000);

    $.ajax({
            type: 'POST',
            url: 'useredit.php',
            data: { 'field' : 'group', 'value' : $(this).val().trim(), 'id' : UserEditedId },
            success : function(result){
                clearTimeout(TimerVar);
                if(result == "INFO1") {
                    UserEditedLog.html(" <span style='color: #c9e52d;'>'group' field updated successfully!</span> ");
                    TimerVar = setTimeout( function(){clearUserEditLog()} ,4000);
                }
                else if(result == "ERROR1") {
                    UserEditedLog.html(" <span style='color: #e52d58;'>failed attempt to update 'group' field!</span> ");
                    TimerVar = setTimeout( function(){clearUserEditLog()} ,4000);
                }
                else {
                    UserEditedLog.html(result);
                }
            }
        });
});

这是用 PHP 动态添加的 HTML:

<!-- Some more dinamicaly added HTML here -->
<select class='SelectLightThemeShort UserEdit_group'>";
    if($row['group'] == "Members") {
        echo "
            <option value='Members' selected='selected'>Members</option>
            <option value='Moderators'>Moderators</option>";
    }
    else if($row['group'] == "Moderators") {
        echo "
            <option value='Members'>Members</option>
            <option value='Moderators' selected='selected'>Moderators</option>";
    }

    echo "
</select>
<!-- Some more dinamicaly added HTML here -->

问题是我使用相同的PHP 脚本和AJAX 脚本(只是使用其他值)来更新其他字段并且它工作得很好。但是当我用它来更新“组”字段时,我得到了这个错误: 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 'group='Members' WHERE id='39'' at line 1 我注意到 39 之后的两个单引号,但我不明白为什么它们在那里,因为这个错误和那个引号仅在我尝试更新“组”字段时出现。 错误1064 MySQL版本5.6.15

【问题讨论】:

    标签: javascript php mysql sql ajax


    【解决方案1】:

    从错误看来,您使用的是保留字 group,如果它没有使用反引号转义,您将收到此错误

    if(mysqli_query($link," UPDATE users SET `$field`='$value' WHERE id='$id' "))
    

    https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

    【讨论】:

    • 谢谢!我会改变它。我认为保留字是问题,但在我检查的列表中我没有找到组字。
    • 是的,它是保留的,我们将其用作select count(*),country from table group by country
    • 如果我使用groups 而不是group 会解决问题吗?
    猜你喜欢
    • 2011-02-11
    • 1970-01-01
    • 2012-01-27
    • 2011-04-17
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多