weixin_33709590 2015-04-24 16:54 采纳率: 0%
浏览 40

用 PHP 脚本和 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:

<!-- 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脚本来更新其他字段时一切运行得,常完美,但当我使用它更新“group”字段时,我会得到以下错误: 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之后有两个单引号,但我不明白它们为什么会出现,因为这个错误和那个引号只有在我试图更新“group”字段时才会出现。Error:1064 我是用的是MySQL版本5.6.15。

  • 写回答

1条回答 默认 最新

  • weixin_33698823 2015-04-24 16:56
    关注

    From the error it appears you are using a reserved word group and if its not escaped using backticks you will get this error

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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀