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);
?>

Dani AI

Generated

Short diagnosis: is right — the update loop is using numeric indexes for the editable fields and a separate array for checked IDs, so the indexes don’t line up when you only check some rows. In plain terms: the checked-list order (only checked rows) is not guaranteed to match the position-based arrays you created for every row, so the script grabs the wrong row’s values and updates the first row.

Minimal, reliable fix — key your inputs by the DB id and iterate the checked ids on submit. Example pattern (front end and back end only — names differ from your original code):

Frontend (per row):

<input type="checkbox" name="selected_ids[]" value="<?= $row['id'] ?>">
<input type="text" name="style_by_id[<?= $row['id'] ?>]" value="<?= htmlspecialchars($row['style']) ?>">
<!-- repeat for other fields using the same id as the key -->

Backend (processing):

if (!empty($_POST['selected_ids'])) {
  foreach ($_POST['selected_ids'] as $id) {
    $id = (int)$id; // validate
    $style = $_POST['style_by_id'][$id];
    // ... update row $id with the fields read by $id ...
  }
}

Other important notes and troubleshooting tips:

  • Remove error_reporting(0) while debugging so you can see warnings. Use var_dump($_POST) early in updateproc to inspect the arrays and confirm mapping.
  • Use htmlspecialchars() when echoing values into form inputs to avoid HTML injection.
  • Use prepared statements (mysqli or PDO) instead of interpolating variables into SQL to prevent SQL injection.
  • Small HTML fix: the size attribute expects a number (e.g., size="4"), not px; use CSS for pixel widths.
  • A simpler alternative is to make checkboxes carry the row index (e.g., chk[$i]) so keys match row inputs, but keying by DB id is clearer and less error-prone.

These changes keep the mapping explicit (checked id → its fields) and will stop the “always update first row” symptom.

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.