未捕获的 PDOException: SQLSTATE[HY093]: 参数编号无效: 未定义参数



未捕获的PDOException:SQLSTATE[HY093]:无效的参数编号:参数未在第73行定义。

这个错误是什么?

$stmtExec = $stmt->execute(); // the line 73

员工.php

public function update($fields,$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 info SET".$st;
$sql.= "WHERE id =".$id;
$stmt = $this->connect()->prepare($sql);
foreach ($fields as $key => $value) {
$stmt->bindValue(':'.$key, $value);
}
$stmtExec = $stmt->execute(); // here is the line with the error
if ($stmtExec) {
header('Location:index.php');
}
}

有一些语法错误。就像SET之后和关键字之前没有空格WHERE。我已经添加并更新了您的代码。

public function update($fields, $id) {
$st = "";
$counter = 1;
$total_fields = count($fields);
foreach ($fields as $key => $value) {
if ($counter === $total_fields) {
$st .= "$key = :" . $key;
} else {
$st .= "$key = :" . $key . ",";
$counter++;
}
}
$sql = "UPDATE info SET " . $st . " WHERE id =" . $id;
$stmt = $this->connect()->prepare($sql);
foreach ($fields as $key => $value) {
$stmt->bindValue(':' . $key, $value);
}
$stmtExec = $stmt->execute();
if ($stmtExec) {
header('Location:index.php');
}
}

最新更新