我有一张桌子,上面写着一个OrderID和订单日期的列表。
<table border="1" style="margin: 10px 0;">
<form action="code/update-to-dispatched.php" method="post" name="markAsDispatched">
<?php
foreach ($orders as $row) {
echo "<tr class='even'>";
echo "<td>";
echo "<strong>Order Date:</strong> ". $row['OrderDate'] ." <br />";
echo "</td>";
echo "<td>";
echo "<strong>Order ID:</strong> ". $row['OrderID'] ."";
echo "</td>";
echo "<td>";
echo "<input type='checkbox' name='chkBox' value='1'>";
echo "</td>";
echo "</tr>";
}
?>
<span class="tag"><a href="javascript:checkall('markAsDispatched','chkBox',true)">CHECK ALL</a></span>
<span class="tag"><a href="javascript:checkall('markAsDispatched','chkBox',false)">UNCHECK ALL</a></span>
<input type="submit" name="markAsDispatched" value="MARK AS DISPATCHED" />
</form>
</table>
如何在成功的代码/ update-to-dispatched.php上运行一条UPDATE语句,以更新要分派的订单的状态(如果选中此复选框)?这是我已经拥有的。
<?php
$servername = "REMOVED";
$username = "REMOVED";
$password = "REMOVED";
$dbname = "REMOVED;
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
try {
if(isset($_POST['chkBox'])) {
$stmt = "UPDATE orders SET Status = :status WHERE OrderID IN (". $_POST['chkBox'] .")";
$stmt = $conn->prepare($stmt);
$stmt->execute(array(':status' => "Dispatched"));
header('Location: /cms/mark-orders-dispatched.php');
}
else {
echo "NO UPDATE";
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
最佳答案
首先,以这样一种方式呈现复选框,以便您可以将其以具有订单ID的数组形式获取。
echo "<input type='checkbox' name='chkBox[]' value='$row['OrderID']'>";
现在,对每个选中的复选框运行查询,如下
$ids=$_POST['chkBox'];
foreach($ids as $id)
{
$stmt = "UPDATE orders SET Status = :status WHERE OrderID=$id";
$stmt = $conn->prepare($stmt);
$stmt->execute(array(':status' => "Dispatched"));
}
header('Location: /cms/mark-orders-dispatched.php');
或简单地
运行您的查询而没有循环为
$ids=$_POST['chkBox'];
$stmt = "UPDATE orders SET Status = :status WHERE OrderID In ".implode(",",$ids);
关于php - 使用TICKED复选框为行运行UPDATE语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33077524/