gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:07:55 25 4
gpt4 key购买 nike

目前卡在尝试配置一个 php 页面以通过选中一个或多个复选框来更新 MySQL 数据库行“orderStatus”。提交表单后,它应该将所选行的 orderStatus 更新为“已验证”。由于 validate.php 片段中的 $orderID = $_POST['id'];,它仅更新表格的第一行。我试图做的是检索已检查行的所有 orderID,并将其分配给变量/数组 $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

感谢任何帮助!

最佳答案

问题是 Form 在 while 循环内部。当您使用 with 时,您需要将标签完全放在 外部,或者将整个标签放在一个 内部。任何其他结构都会破坏 的语法,并且将被浏览器忽略或呈现不正确。

请试试这个

<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>

关于PHP 表单 - 使用一个或多个复选框更新数据库行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41053785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com