我的php crud(面向对象和pdo)更新不起作用。它显示了
致命错误:未捕获的pdoexception:sqlstate[hy093]:无效的参数号:未定义参数。在$stmtexec=$stmt->execute()上;
我试图改变变量和参数,参数和间距,但仍然得到一个无效的结果。我怎样才能解决这个问题?
public function updateId($fields, $stud_id) {
$st = "";
$counter = 1;
$total_fields = count($fields);
foreach($fields as $key => $value){
if($counter == $total_fields){
$set = "$key = :".$key;
$st = $st.$set;
} else {
$set = "$key = :".$key.", ";
$st = $st.$set;
$counter++;
}
}
$sql = "";
$sql.= "UPDATE students SET ".$st;
$sql.= "WHERE id = ".$stud_id;
$stmt = $this->connect()->prepare($sql);
foreach($fields as $key => $value){
$stmt->bindValue(':'.$key, $value);
}
$stmtExec = $stmt->execute();
//if successful
if($stmtExec){
header('Location: index.hp');
}
}
1条答案
按热度按时间ss2ws0br1#
如果我正确理解了您的代码,您需要在此处添加一个空格:
$set = "$key = :".$key." ";
如果你错过了那个空间,关键字WHERE
将是最后一个参数名的一部分。