我的脚本尝试在$ system_id具有特定值的情况下更新数据库值;问题在于,即使$ system_id的值错误,它也会更新数据库值。
更具体地说:$ g1choice,$ g2choice和$ g3choice将针对$ factor匹配的任何行进行更新,而与$ system_id无关
我在做什么错?谢谢! :)
以下:1)代码; 2)尝试修复代码失败
//Insert & Update Factor A table
$page_query = $pdo->query(
"SELECT sum(task_1) AS task01, sum(task_2) AS task02, sum(task_3) AS task03, rand01
FROM pages
GROUP BY rand01"
);
while ($row = $page_query->fetch()) {
$factors = $pdo->prepare(
"SELECT factor
FROM factorA
WHERE factor = ? && system_id = ?"
);
$factors->execute([$row['rand01'], $system_id]);
$count = count($factors->fetchAll());
if ($count > 0) {
$pdo->prepare(
"UPDATE factorA
SET g1choice = ?, g2choice = ?, g3choice = ?
WHERE factor = ?"
/* failed fix: WHERE factor = ? && system_id = ?" */
)->execute([$row['task01'], $row['task02'], $row['task03'], $row['rand01']]);
/* failed fix: )->execute([$row['task01'], $row['task02'], $row['task03'], $row['rand01'],$system_id]); */
} else {
$pdo->prepare(
"INSERT INTO factorA (system_id, factor, g1choice, g2choice, g3choice)
VALUES (?,?,?,?,?)"
)->execute([$system_id, $row['rand01'], $row['task01'], $row['task02'], $row['task03']]);
}
}
/*failed fixes below*/
//failed fix (1):
failed fix:
$page_query = $pdo->prepare(
"SELECT sum(task_1) AS task01,
sum(task_2) AS task02, sum(task_3)
AS task03, rand01
FROM pages
WHERE system_id= ?
GROUP BY rand01"
);
$factors->execute([$system_id]);
*/
//failed fix (2)
/*
if ($count > 0) {
$pdo->prepare(
"UPDATE factorA
SET g1choice = ?, g2choice = ?, g3choice = ?
was WHERE factor = ?"
/* failed fix: WHERE factor = ? && system_id = ?" */
)->execute([$row['task01'], $row['task02'], $row['task03'], $row['rand01']]);
/* failed fix: )->execute([$row['task01'], $row['task02'], $row['task03'], $row['rand01'],$system_id]); */
*/