PHP表单 - 使用一个或多个复选框更新数据库行

时间:2016-12-09 05:40:38

标签: php html mysql forms checkbox

目前卡住尝试通过选中一个或多个复选框来配置php页面以更新MySQL数据库行“orderStatus”。一旦提交表单,它应该将所选行的orderStatus更新为“validated”。它只更新表格的第一行,可能是由于validate.php片段中的 $ orderID = $ _POST ['id']; 。我试图做的是检索已检查的行的所有orderID,并将其分配给变量/ array $ orderID,以便仅为这些行更改orderStatus。

这是HTML:

<html>
<header>
<title>Validate an Order</title>

</header>
<body>

<h1>Validate an Order</h1>
<h4>Showing all unvalidated orders.</h4>
<br/>

<?php
$con=mysqli_connect("***","***","***","***");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM orders WHERE orderStatus = 'processing' ORDER BY orderDate DESC");

echo "<table border='1'>
<tr>
<th>Validate?</th>
<th>OrderID</th>
<th>OrderDate</th>
<th>OrderShipDate</th>
<th>OrderType</th>
<th>OrderMedia</th>
<th>OrderContent</th>
<th>OrderStatus</th>
<th>OrderQuantity</th>
<th>OrderCost</th>
<th>OrderDeposit</th>
<th>OrderDesc</th>
</tr>";


while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<form action='validate.php' method='post'><input type='hidden' name='id' value='".$row['orderID']."'><input type='checkbox' name='validate[]' value='validated'>" . "</td>";
echo "<td>" . $row['orderID'] . "</td>";
echo "<td>" . $row['orderDate'] . "</td>";
echo "<td>" . $row['orderShipDate'] . "</td>";
echo "<td>" . $row['orderType'] . "</td>";
echo "<td>" . $row['orderMedia'] . "</td>";
echo "<td>" . $row['orderContent'] . "</td>";
echo "<td>" . $row['orderStatus'] . "</td>";
echo "<td>" . $row['orderQuantity'] . "</td>";
echo "<td>" . $row['orderCost'] . "</td>";
echo "<td>" . $row['orderDeposit'] . "</td>";
echo "<td>" . $row['orderDesc'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' name='submit' value='Submit'></form>";
mysqli_close($con);
?>


</body>
</html>

以下是表单的PHP:

<?php
$con=mysqli_connect("***","***","***","***");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$orderID = $_POST['id'];

    if(isset($_POST['validate'])){
    foreach($_POST['validate'] as $validate){
       mysqli_query($con,"UPDATE orders SET orderStatus = '$validate' WHERE orderID = $orderID");
    }
}

mysqli_close($con);

?>

这是页面的样子。

Validate an Order

感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

问题是Form在while循环中。当你使用时,你需要将标签完全放在外面,或者整个内部标签。任何其他结构都会破坏它的语法,浏览器会忽略它,或者渲染不正确。

请试试这个

<html>
<header>
<title>Validate an Order</title>

</header>
<body>

<h1>Validate an Order</h1>
<h4>Showing all unvalidated orders.</h4>
<br/>

<?php
$con=mysqli_connect("***","***","***","***");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM orders WHERE orderStatus = 'processing' ORDER BY orderDate DESC");
echo "<form action='validate.php' method='post'>
<table border='1'>
<tr>
<th>Validate?</th>
<th>OrderID</th>
<th>OrderDate</th>
<th>OrderShipDate</th>
<th>OrderType</th>
<th>OrderMedia</th>
<th>OrderContent</th>
<th>OrderStatus</th>
<th>OrderQuantity</th>
<th>OrderCost</th>
<th>OrderDeposit</th>
<th>OrderDesc</th>
</tr>";


while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='hidden' name='id' value='".$row['orderID']."'><input type='checkbox' name='validate[]' value='validated'>" . "</td>";
echo "<td>" . $row['orderID'] . "</td>";
echo "<td>" . $row['orderDate'] . "</td>";
echo "<td>" . $row['orderShipDate'] . "</td>";
echo "<td>" . $row['orderType'] . "</td>";
echo "<td>" . $row['orderMedia'] . "</td>";
echo "<td>" . $row['orderContent'] . "</td>";
echo "<td>" . $row['orderStatus'] . "</td>";
echo "<td>" . $row['orderQuantity'] . "</td>";
echo "<td>" . $row['orderCost'] . "</td>";
echo "<td>" . $row['orderDeposit'] . "</td>";
echo "<td>" . $row['orderDesc'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' name='submit' value='Submit'>"
. "</form>";
mysqli_close($con);
?>


</body>
</html>

答案 1 :(得分:0)

谢谢大家!我最后将HTML中的输入复选框标记更改为:<input type="checkbox" name="vid[]" id="validate" value='.$row['orderID'].'这会将选中的行的orderID扫描到vid []数组中。

然后在validate.php中,查询将状态设置为&#34;验证&#34;使用来自foreach()的$ vid:

// if the vid array exists
if(isset($_POST['vid'])) {

// Loop through vid array "containing orderIDs" and set orderStatus to "validated" only for those orderIDs
foreach($_POST['vid'] as $vid) {
mysqli_query($con,"UPDATE orders SET orderStatus = 'validated' WHERE orderID = '$vid'");
}
}