我是PDO的新手,我在更新数据库中的数组值方面遇到了问题。除了从数组中更新多个值外,一切正常。
使用PHP代码的HTML:
<?php
$next = 0;
echo '<ul>';
$sql = "SELECT * FROM customers LEFT JOIN products ON customers.product_code = products.product_code RIGHT JOIN transactions ON customers.transcode = transactions.transcode WHERE transactions.id = '$id'";
foreach ($pdo->query($sql) as $row) {
echo '<li>'.$row['product_name'] .' x <input name="p_qty['.$next++.']" type="number" value="'. $row['p_quantity'] .'"></li>';
}
echo '</ul>';
?>
输出:
每当我尝试更改第三个结果的数量时,第一个和第二个数量也会跟随。
这是PDO代码:
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE transactions JOIN customers ON transactions.transcode = customers.transcode SET transactions.transcode = ?, date = ?, customers.custname = ?, payment = ?, status = ?, customers.p_quantity = ? WHERE transactions.id = ?";
$q = $pdo->prepare($sql);
foreach($_POST['p_qty'] as $qty){
$q->execute(array($transcode, $date, $custname, $payment, $status, $qty, $id));
}
Database::disconnect();
header("Location: index.php");
答案 0 :(得分:0)
您的select语句会为相关的交易ID提取这3行。
您的更新语句的where子句也仅通过事务ID拉动,这意味着它将匹配这3行。
如果您希望更新语句仅更改其中一个产品的数量,则需要将其他字段传递给where子句。