I have a form with many rows. I want to allow users to update a selection of rows by checking a checkbox in each of the rows they wish to to update.
The code works reliably for single line updates but when multiple lines are chosen the data updated is not from the relevant rows. I.e., the checkboxes work correctly in that only rows with a checked checkbox are updated but if there are multiple checked rows values are not from the correct row (but from some other row - checked or not - in the form).
Here are the relevant form elements:
<tr class='job-row recent<?php echo $editRecent; ?>'>
<td class="jobnr-cell table-tooltip"><input type="checkbox" value="<?php echo $row['jobnr']; ?>" name="editCheck[]"/><input id='hiddenCheck' type='hidden' value='0' name='editCheck[]'> <a href='editOpg.php?jobnr=<?php echo $row['jobnr']; ?>&month=<?php echo $monthno; ?>&plx=<?php echo urlencode($plx); ?>'><?php echo $row['jobnr']; ?></a><span class="table-tooltiptext"><?php echo $projekt; ?></span></td>
<input type="hidden" name="jobnr[]" value="<?php echo $row['jobnr']; ?>">
<td class="job-cell name-cell"><?php echo $knshort; ?></td>
<!-- <td><?php echo $projekt; ?></td> -->
<td class="job-cell name-cell"><?php echo $kundeansvarlig; ?></td>
<!-- <td class="job-cell name-cell"><a href='editJobs.php?plx=<?php echo urlencode($row['projektleder']); ?>'><?php echo $projektleder; ?></a></td> -->
<td class="job-cell"><div class="UI_cell"><input type="text" class="UI_input" name="tilbudspris" id="tilbudspris<?php echo $row['jobnr']; ?>" value="<?php echo $row['tilbudspris']; ?>" readonly> kr.</div></td>
<input type="hidden" class="UI_input" name="via0" id="via0<?php echo $row['jobnr']; ?>" value="<?php echo $row['via0']; ?>" readonly>
<td class="job-cell via-tooltip UI_cell">
<input type="text" class="UI_input border-top" name="bookedMonth" id="bookedMonth<?php echo $row['jobnr']; ?>" value="<?php echo $bookedMonth; ?>" readonly> kr.
<span class="via-tooltiptext"><?php echo $row['viadd']; ?> kr. - <?php echo $row['viadd']; ?> kr.</span></td>
<td class="job-cell UI_edit"><div class="UI_cell"><input type="text" class="UI_input UI_edit" name="forbrug[]" id="forbrug<?php echo $row['jobnr']; ?>" value="<?php echo $row['UI_forbrug']; ?>"> kr.</div></td>
<td class="job-cell UI_edit"><div class="UI_cell"><input type="text" class="UI_input UI_edit" name="rekvisitioner[]" id="rekvisitioner<?php echo $row['jobnr']; ?>" value="<?php echo $row['UI_rekvisitioner']; ?>"> kr.</div></td>
<td class="job-cell via-tooltip UI_cell"><input type="text" class="UI_input" name="forvEXreg" id="forvEXreg<?php echo $row['jobnr']; ?>" value="<?php echo $forvEXreg; ?>" readonly> kr.
<span class="forv-tooltiptext"><input type="text" class="UI_input" name="restValTilbud" id="restValTilbud<?php echo $row['jobnr']; ?>" value="<?php echo $restValTilbud; ?>" readonly> kr.</span></td>
<td class="job-cell"><div class="UI_cell"><?php echo $row['reguleringer']; ?> kr.</div></td>
<td class="job-cell UI_edit"><div class="UI_cell"><input type="text" class="UI_input UI_edit" name="reguleringer[]" id="reguleringer<?php echo $row['jobnr']; ?>" value="<?php echo $row['UI_reguleringer']; ?>"> kr.</div></td>
<td class="job-cell"><div class="UI_cell"><input type="text" class="UI_input" name="forvINCreg" id="forvINCreg<?php echo $row['jobnr']; ?>" value="<?php echo $forvINCreg; ?>" readonly> kr.</div></td>
<td class="job-cell"><div class="UI_cell"><input type="text" class="UI_input" name="forecast" id="forecast<?php echo $row['jobnr']; ?>" value="<?php echo $forecast; ?>" readonly> kr.</div></td>
<td class="job-cell"><div class="UI_cell"><input type="text" class="UI_input" name="restValNextMonths" id="restValNextMonths<?php echo $row['jobnr']; ?>" value="<?php echo $restValNextMonths; ?>" readonly> kr.</div></td>
<td class="job-cell UI_edit"><div class="UI_cell"><input type="text" class="UI_input UI_edit" name="forecast2[]" id="forecast2<?php echo $row['jobnr']; ?>" value="<?php echo $row['UI_forecast2']; ?>"> kr.</div></td>
</tr>
And here is how I process it:
$monthno = $_GET['month'];
$weekno = $_GET['week'];
$yearno = $_GET['year'];
$editCheck = $_POST['editCheck'];
$jobnr = $_POST['jobnr'];
$forbrug = $_POST['forbrug'];
$rekvisitioner = $_POST['rekvisitioner'];
$reguleringer = $_POST['reguleringer'];
$forecast2 = $_POST['forecast2'];
$chkcount = count($jobnr);
for($i=0; $i<$chkcount; $i++)
{
$con->query("UPDATE jobs SET UI_forbrug='$forbrug[$i]', UI_rekvisitioner='$rekvisitioner[$i]', UI_reguleringer='$reguleringer[$i]', UI_forecast2='$forecast2[$i]', edate = NOW() WHERE monthno = $monthno AND jobnr=".$editCheck[$i]);
}