我无法弄清楚为什么受影响的行数为0.我认为在我尝试执行的查询中存在语法或类型不匹配。我试图通过将$querystring
替换为col1=test, col2=test
来手动执行此操作,但它可以工作但不能使用字符串变量。我做错了什么想法?
以下是代码:
$mysqli = new mysqli($servername, $username, $password, $database);
mysqli_set_charset($mysqli,"utf8");
$tablename = $_POST['table'];
$tableid = (int)$_POST['id'];
$datatoupdate = array_diff_key($_POST, (array('table'=>'', 'id'=>'')));
foreach ($datatoupdate as $column => $value) {
$colsandvals[] = "`" . $column . "`" . "=" . "'" . $value . "'";
}
$querystring = implode(', ' , $colsandvals);
$result = $mysqli->query("UPDATE $tablename SET $querystring WHERE `id`=$tableid") or die(mysqli_error($mysqli));
echo ($result); //output is 1
echo ($mysqli->affected_rows); //output is 0
编辑:我特意省略了处理SQL注入的代码。