【问题标题】:PHP Form - Update database row using one or more checkboxesPHP 表单 - 使用一个或多个复选框更新数据库行
【发布时间】:2016-12-16 11:47:45
【问题描述】:

目前卡在尝试通过选中一个或多个复选框来配置 php 页面以更新 MySQL 数据库行“orderStatus”。提交表单后,它应该将所选行的 orderStatus 更新为“已验证”。由于 validate.php sn-p 中的 $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

感谢任何帮助!

【问题讨论】:

  • 请 print_r($_POST['validate']) 是否回显所有值?
  • 您正在为每个 $row 循环创建一个 &lt;form&gt;。由于您只在表单中发送 1 个 validate 值,这就是为什么只更新第一个值。

标签: php html mysql forms checkbox


【解决方案1】:

问题是 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>

【讨论】:

  • 我没有检查天气这个代码是否可以工作,但我假设你的表单可以工作,但你仍然需要更正他的 validate.php,提示 - 只需添加$orderId 中的索引,因此当循环返回时它会增加。我认为这会很好
  • @arif_suhail_123-对不起,我没听懂你……你能编辑我的代码吗?
  • 不,我不想碰,因为我不知道还有什么问题,我只是说这个 $orderId 不是单个值,它是一个数组,如果你看到图片,所以当你运行您需要处理的查询。他的查询就像这里的mysqli_query($con,"UPDATE orders SET orderStatus = '$validate' WHERE orderID = $orderID"); 在这里orderID,是单个值,他应该像这样在循环之前声明变量$i,然后运行这个查询。 mysqli_query($con,"UPDATE orders SET orderStatus = '$validate' WHERE orderID = '".$orderID[$i++]."'"); so the each time pointer moves
【解决方案2】:

谢谢大家!我最终将 HTML 中的输入复选框标记更改为:&lt;input type="checkbox" name="vid[]" id="validate" value='.$row['orderID'].' 这将抓取那些选择到 vid[] 数组中的行的 orderID。

然后在 validate.php 中,查询使用 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'");
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2012-06-03
    • 2014-06-23
    • 2012-08-16
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多