我有如下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。