我在php页面中添加了一个功能,当我按下“提交”按钮时,“已确认”列的值从0更改为1,但是它一次更改了每条记录的“已确认”值,而我只想对一条记录执行此操作时间,并且记录具有唯一的销售ID。
代码:
<?php
$conn = $pdo->open();
try{
$stmt = $conn->prepare("SELECT *, sales.id AS salesid FROM sales LEFT JOIN users ON users.id=sales.user_id ORDER BY sales_date DESC");
$stmt->execute();
foreach($stmt as $row){
$stmt = $conn->prepare("SELECT * FROM details LEFT JOIN products ON products.id=details.product_id WHERE details.sales_id=:id");
$stmt->execute(['id'=>$row['salesid']]);
$total = 0;
foreach($stmt as $details){
$subtotal = $details['price']*$details['quantity'];
$total += $subtotal;
}
if($row['Confirmed'] == 0){
echo "
<tr>
<td class='hidden'></td>
<td>".date('M d, Y', strtotime($row['sales_date']))."</td>
<td>".$row['firstname'].' '.$row['lastname']."</td>
<td>".$row['country'].' , '.$row['city']." , ".$row['address']."</td>
<td width='25px;'>₺ ".number_format($total, 2)."</td>
<td width='18px;'><button type='button' class='btn btn-info btn-sm btn-flat transact' data-id='".$row['salesid']."'><i class='fa fa-search'></i> View</button></td>
<form action='' method='post'>
<td width='18px;'>
<input type='hidden' name='confirm' value='1'> <input class='btn btn-success btn-sm btn-flat' type='submit' value='Confirm'>
</td>
</form>
</tr>
";
if(isset($_POST['confirm']) && $_POST['confirm'])
{
$stmt = $conn->prepare("UPDATE sales SET Confirmed = 1 WHERE id = '".$row['salesid']."'");
$stmt->execute();
echo "<meta http-equiv='refresh' content='0'>";
}
}
}
}
catch(PDOException $e){
echo $e->getMessage();
}
$pdo->close();
?>