Hi I'm trying to update my DB table with below code.When I select all row or only 1st row its working fine, but when I select 2nd row or another row, its updating the 1st row value to my DB table. Please see & try to help me.

update.php

<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr><td>

<form name="namestoupdate" method="post" action="updateproc.php">
<table width="100%" border="0" cellspacing="1" cellpadding="1"> 
<tr bgcolor="#FDEEF4">
<th align="center"><input type="checkbox"  id="selectall" onClick="selectAll(this)" ></th>
<th align="center">ID</th>
<th align="center">ExcelID</th>
<th align="center">STYLE</th>
<th align="center">ORDER NO</th>

<th align="center"><strong>COLOR</strong></th>
<th align="center"><strong>SIZE</strong></th>
<th align="center"><strong>QTY</strong></th>
<th align="center"><strong>CTN-QTY</strong></th>
<th align="center"><strong>INVOICE</strong></th>
<th align="center">KCGMT</th>
<th align="center">BUYER</th>
<th align="center">FACTORY</th>
</tr>

<?php
  include 'db_connection.php';

  $sql = "SELECT * FROM freddyshipment WHERE style='$style' AND invoice='$invoice' AND kcgmt='$kcgmt' AND buyer='$buyer' AND factory='$factory'";
  $result = mysqli_query($conn,$sql) or die (mysqli_error($conn));
  $size = count($_POST['chk']);
  $i = 0;
  while ($Update = mysqli_fetch_array($result)) {
  print "</tr>\n";
  print "<td align='center'><input type='checkbox' name='chk[]' value='{$Update['id']}'/></td>";
  print "<td align='center'>{$Update['id']}</td>";
  print "<td align='center'><input type='text' size='4px' name='excelid[$i]' value='{$Update['excelid']}' /></td>";
  print "<td align='center'><input type='text' size='18px' name='style[$i]' value='{$Update['style']}' /></td>";
  print "<td align='center'><input type='text' size='4px' name='orderno[$i]' value='{$Update['orderno']}' /></td>";
  print "<td align='center'><input type='text' size='3px' name='col[$i]' value='{$Update['col']}' /></td>";
  print "<td align='center'><input type='text' size='2px' name='sizes[$i]' value='{$Update['sizes']}' /></td>\n";
  print "<td align='center'><input type='text' size='3px' name='qty[$i]' value='{$Update['qty']}' /></td>\n";
  print "<td align='center'><input type='text' size='3px' name='ctnqty[$i]' value='{$Update['ctnqty']}' /></td>\n";
  print "<td align='center'><input type='text' size='7px' name='invoice[$i]' value='{$Update['invoice']}' /></td>\n";  
  print "<td align='center'><input type='text' size='7px' name='kcgmt[$i]' value='{$Update['kcgmt']}' /></td>";
  print "<td align='center'><input type='text' size='7px' name='buyer[$i]' value='{$Update['buyer']}' /></td>";
  print "<td align='center'><input type='text' size='7px' name='factory[$i]' value='{$Update['factory']}' /></td>";
  print "</tr>\n";
  ++$i;
  }
  echo mysqli_error($conn); 
  //mysql_close();
?>
<tr> <td colspan="12" align="center"><input type="submit" name="btn" value="Update"></td></tr>";
</table>
</td>
</tr>
</form>
</table>

updateproc.php

<?php
error_reporting(0);
include_once('db_connection.php');

if(isset($_POST['btn'])){
 if(!empty($_POST['chk'])){ 
echo $size = count($_POST['chk']);
echo "-";
$i = 0;
while ($i < $size) {
echo $id = $_POST['chk'][$i];
$excelid = $_POST['excelid'][$i];
$style = $_POST['style'][$i];
$orderno = $_POST['orderno'][$i];
$col = $_POST['col'][$i];
$sizes = $_POST['sizes'][$i];
$qty = $_POST['qty'][$i];
$ctnqty = $_POST['ctnqty'][$i];
$invoice = $_POST['invoice'][$i];
$kcgmt = $_POST['kcgmt'][$i];
$buyer = $_POST['buyer'][$i];
$factory = $_POST['factory'][$i];

$query = "UPDATE `freddyshipment` SET 

excelid = '$excelid', 
style = '$style',
orderno = '$orderno', 
col = '$col',
sizes = '$sizes',
qty = '$qty',
ctnqty = '$ctnqty',
invoice = '$invoice',
kcgmt = '$kcgmt',
buyer = '$buyer',
factory = '$factory'
WHERE `id` = '$id' LIMIT 1";
$udateq=mysqli_query($conn,$query) or die ("Error in query: $query");

print "
<tr>
<td align='center'><p>$id</p></td>
<td align='center'>$excelid</td>
<td align='center'>$style</td>
<td align='center'>$orderno</td>
<td align='center'>$col</td>
<td align='center'>$sizes</td>
<td align='center'>$qty</td>
<td align='center'>$ctnqty</td>
<td align='center'>$invoice</td>
<td align='center'>$kcgmt</td>
<td align='center'>$buyer</td>
<td align='center'>$factory</td>
</tr>
";
++$i;
}
 }
    else
    {
        echo "Please Select Check Box";
    }

    }
mysqli_close($conn);
?>

The only thing that can be going wrong is that the ID is getting misread when looping through an array with a single value. It is always reading the ID of the first row regardless which checkbox was actually checked.
You'll need to do some debugging to figure exactly why that is happening but I'd start with looking at the POST variables going to the update script. That should show you the way forward.

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.