我正在使用此代码在网络应用程序上输入更新用户权限:
$updaters = array();
for ($i = 1; $i <= 24; $i++){
if (isset($_POST['permsA['.$i.']']))
$updaters[] = '`'.$i.'` = \''.mysqli_real_escape_string($db, $_POST['permsA['.$i.']']).'\'';
}
$insert = mysqli_query($db,'UPDATE `tbl_perms` SET '.implode(',', $updaters). 'WHERE `userid` = '.$id)or die(mysqli_error($db));
我得到的错误来自 SQL:
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 'WHERE `userid` = 1' at line 1
I can't seem to be able to rewrite the code to remove the error :(
this is the code to generate permsA
<?
while($i = mysqli_fetch_array($get_perms)){
$pname = $i[pname];
$id = $i[id];
?>
<div><input type="checkbox" tabindex="1" name="permsA[<? echo $id;?>]" value="1" <? if($permissionid[$id] == '1') {echo ' checked="checked" ';}?> /><?echo htmlspecialchars($pname);?></div>
<? } ?>
这是产生错误的确切查询:
$insert = mysqli_query($db,'UPDATE `tbl_perms` '.$updaters.' WHERE `userid` = '.$id) or die(mysqli_error($db));
这是此 sql 生成的内容:
UPDATE `tbl_perms` SET WHERE `userid` = 1
这里是 POST var_dump($_POST['permsA'])
之后变量的变量转储:
array(22) { [1]=> string(1) "1"[2]=> string(1) "1"[3]=> string(1) "1"[4] => 字符串(1)“1”[5]=> 字符串(1)“1”[6]=> 字符串(1)“1”[8]=> 字符串(1)“1”[9]=>字符串(1)“1”[10]=>字符串(1)“1”[11]=>字符串(1)“1”[12]=>字符串(1)“1”[13]=>字符串( 1)“1”[14]=>字符串(1)“1”[15]=>字符串(1)“1”[16]=>字符串(1)“1”[17]=>字符串(1) “1”[18]=> 字符串(1)“1”[19]=> 字符串(1)“1”[20]=> 字符串(1)“1”[21]=> 字符串(1)“1 "[22]=> 字符串(1) "1"[23]=> 字符串(1) "1"}
最佳答案
在 WHERE 子句之前但在撇号 ' WHERE
之后添加一个空格
关于php - MySQL错误,我看不出哪里出了问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1710726/