$query = "update project_staff
set
id_employee=?
where
id_project=?
and
id_projectemployee=?
";
$stmt = $this->dbh->prepare($query);
for ($i = 0; $i < count($_POST['id_employee']); $i++){
$employee = $_POST['id_employee'][$i];
$stmt->bindValue(1, $employee, PDO::PARAM_INT);
$stmt->bindValue(2, $_POST["id"], PDO::PARAM_INT);
$stmt->bindValue(3, $_POST["idstaff"], PDO::PARAM_INT);
$stmt->execute();
}
echo("Project" . " " . $_POST["nom"] . " ". "has been updated");
答案 0 :(得分:1)
您正在将绑定参数设置为显式数据类型PDO::PARAM_INT
,但值FROM $ _POST始终为字符串。
您应首先更改并验证POST
输入为INT
,并将其更改为INT
或使用PDO::PARAM_STR
// UPDATE
查看代码,将where子句的值设置为$ _POST ['id']和$ _POST [“idstaff”]的内容。因此,在您的foreach的每次运行中,您都要求相同的值。所以$ employee在每次运行时都会更新,但where子句总是相同的。这看起来像是:
//example
//$_POST['id'] = 20;
//$_POST['idstaff'] = 40;
//$_POST['id_employee'][0] = 1;
//$_POST['id_employee'][1] = 2;
//$_POST['id_employee'][2] = 3;
//run 1
$employee = $_POST['id_employee'][0]; //1
update project_staff set id_employee=1 where id_project=20 and id_projectemployee=40
//run 1
$employee = $_POST['id_employee'][1]; //2
update project_staff set id_employee=2 where id_project=20 and id_projectemployee=40
//run 1
$employee = $_POST['id_employee'][2]; //3
update project_staff set id_employee=3 where id_project=20 and id_projectemployee=40
所以你要在每次运行时覆盖以前的更改。