MySQL Errno 1064

时间:2015-04-24 16:54:43

标签: javascript php mysql sql ajax

我有这个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 dinamicaly:

<!-- 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脚本(仅与其他值一起)来更新其他字段并且它完美地工作。但当我用它来更新&#34;组&#34;字段我收到此错误:  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之后的两个单引号,但是我无法理解为什么它们在那里,因为只有当我尝试更新&#34;组&#34;时才出现这个错误和引用。领域。 Errno 1064 MySQL版本5.6.15

1 个答案:

答案 0 :(得分:3)

从错误中看,您使用的是保留字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