PHP Form - Update database row using one or more checkboxes
<p>Currently stuck trying to configure a php page to update a MySQL database row "orderStatus" by checking one or more checkboxes. It should update the orderStatus of the selected row(s) to "validated" once the form is submitted. It's updating only the first row of the table likely due to <strong>$orderID = $_POST['id'];</strong> in the validate.php snippet. What I tried to do is retrieve all the orderIDs of the rows that have been checked and assign it to the variable/array $orderID, so that the orderStatus is changed only for those rows.</p>

<p>Here is the HTML:</p>

<pre><code>&lt;html&gt;
&lt;header&gt;
&lt;title&gt;Validate an Order&lt;/title&gt;

&lt;/header&gt;
&lt;body&gt;

&lt;h1&gt;Validate an Order&lt;/h1&gt;
&lt;h4&gt;Showing all unvalidated orders.&lt;/h4&gt;
&lt;br/&gt;

&lt;?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 "&lt;table border='1'&gt;
&lt;tr&gt;
&lt;th&gt;Validate?&lt;/th&gt;
&lt;th&gt;OrderID&lt;/th&gt;
&lt;th&gt;OrderDate&lt;/th&gt;
&lt;th&gt;OrderShipDate&lt;/th&gt;
&lt;th&gt;OrderType&lt;/th&gt;
&lt;th&gt;OrderMedia&lt;/th&gt;
&lt;th&gt;OrderContent&lt;/th&gt;
&lt;th&gt;OrderStatus&lt;/th&gt;
&lt;th&gt;OrderQuantity&lt;/th&gt;
&lt;th&gt;OrderCost&lt;/th&gt;
&lt;th&gt;OrderDeposit&lt;/th&gt;
&lt;th&gt;OrderDesc&lt;/th&gt;
&lt;/tr&gt;";


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


&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>And here is the PHP for the form:</p>

<pre><code>&lt;?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);

?&gt;
</code></pre>

<p>Here's what the page looks like.</p>

<p><a href="https://i.stack.imgur.com/a1MQG.jpg" rel="nofollow noreferrer">Validate an Order</a></p>

<p>Any help is appreciated!</p>