I'm trying to work on an inventory system where users may view their inventory and update quantity with the value that input by user only and rest remains the same from database. But its not working please help me find where I did wrong. It will echo the success message but the database isn't updated.
<form name="form" method="post">
<table width="70%" border="5" align="center"><tr>
<th scope="row">SKU</th>
<th scope="row">Item Description</th>
<th scope="row">Current Qunatity</th>
<th scope="row">Update Quantity</th>
<th scope="row">Unit Price</th>
</tr>
<tr>
<th scope="row">
<?php
include('connect.php');
$result = mysqli_query("SELECT * FROM products")
or die(mysqli_error());
while($row = mysqli_fetch_array( $result )) {
echo "<tr>";
echo '<td><a name="sku[]">'.$row['sku_id'].'</a></td>';
echo '<td>'.$row['description'].'</td>';
echo '<td>'.$row['quantity'].'</td>';
echo '<td><input name="qty[]" /></td>';
echo '<td>'.$row['unit_price'].'</td>';
echo "</tr>";
}
?>
</table>
<input style="float:right" name="update" type="submit" id="update" value="Update"/>
</form>
<?php
if(isset($_POST['update']))
{
$qty = $_POST['qty'];
$sku = $_POST['sku'];
foreach($qty as $key => $value)
{
if(empty($value))
{
continue;
}
else
{
$sql = "UPDATE products SET quantity ='".$value."' WHERE sku_id = '".$sku[$key]."'";
mysql_query($sql);
}
}
$retval = mysqli_query($sql);
if(! $retval)
{
die('Could not update data: '. mysql_error());
}
echo 'Update data successfully!';
}
?>