Hello, I want to use the checkbox to update the status of a row in mysql using php. If it is checked and submitted, the status will change from 'collect' to 'received'. I tried many ways but I am not sure where the problem is :( please help!

<?php 
include(config.php);

$query = "SELECT * FROM deliveredGoods";
$result = mysqli_query($query);

if ($result === false){
    die(mysqli_error());
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Update Goods Status</title>
<style>
body {
font-family:Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", "serif";
}

h2{
text-align: center;
font-weight:normal;
}

table{
border-collapse: collapse;
width: 100%;
}

tr, th {
border: 1.5px solid #C0C0C0;
padding:5px;
}
</style>    
</head>

<body>
<form method='post' action='#'>
<h2>Update Goods Status</h2>

<table>
    <tr>
    <th>Goods ID</th>
    <th>GoodsListID</th>        
    <th>Resident ID</th>
    <th>Tracking Number</th>
    <th>Company Name</th>
    <th>Arrival Date</th>
    <th>Date To Collect</th>
    <th>Status</th>
    <th>Action
        (mark as received)</th> 
    </tr>

<?php
    while ($row = mysql_fetch_array($result)) {

    $row_id=$row['goodsID'];
    $goodsListID=$row['goodsListID'];
    $residentID=$row['residentID'];
    $trackingNum=$row['trackingNum'];
    $companyName=$row['companyName'];
    $arrivalDate=$row['arrivalDate'];
    $collectDate=$row['collectDate'];
    $status=$row['status'];

echo"<tr>
    <td><?php echo $goodsID; ?></td>
    <td><?php echo $goodsListID; ?></td>
    <td><?php echo $residentID; ?></td>
    <td><?php echo $trackingNum; ?></td>
    <td><?php echo $companyName; ?></td>
    <td><?php echo $arrivalDate; ?></td>
    <td><?php echo $collectDate; ?></td>
    <td><?php echo $status; ?></td>
    <td><?php echo "<input type='checkbox' name='goodsID[]' value='".$row_id."'>"; ?></td>
    </tr>";
    }
</table>
<input type='submit' name='submit' value='Update'/>
</form>
</body>
</html>

<?php
if(gettype($_POST['goodsID'])=="submit"){
foreach($_POST['goodsID'] as $value){
    $goodsID_c = $value;
    $query2 = "UPDATE 'deliveredGoods' SET status = 'Received' where goodsID ='"/$goodsID_c."'";
    $result2 = mysqli_query($query2);
    if ($result2 === false){
        die(mysqli_error());
    }
    echo "Status for .$goodsID_c. has been updated.<br>";
    header(location: goodsList.php);
}

}
mysqli_close();
?>

This was marked as solved yet I don't see the solution.

To me Line 90 seems to set the status to Received regardless of the checkbox value.

I think rproffitt is pointing out that you may have a typo where you put a / character before $goodsID_c.

That being said, you have a whole bunch of things wrong here.

Firstly, do not ever not escape user-defined variables passed into MySQL. On line 89, you should be using:

$goodsID_c = mysqli_real_escape_string($mysql_link_identifier, $value);

All that being said, where is your $mysql_link_identifier (the one generated when you connect to MySQL)? You should be passing it in as the first parameter into your mysqli_query() as demonstrated here: https://www.php.net/manual/en/mysqli.query.php

Then, you are using mysqli_query() with mysql_fetch_array() instead of mysqli_fetch_array(). Those are two separate database libraries.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.