php - Update selected rows from form with checkbox - data from wrong rows added -


i have form many rows. want allow users update selection of rows checking checkbox in each of rows wish to update.

the code works reliably single line updates when multiple lines chosen data updated not relevant rows. i.e., checkboxes work correctly in rows checked checkbox updated if there multiple checked rows values not correct row (but other row - checked or not - in form).

here 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 how 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() monthno = $monthno , jobnr=".$editcheck[$i]);  } 

there's no point having hidden inputs, checkbox input elements fine. here problem is, you're not associating input text rows corresponding checkbox inputs, that's why correct rows(corresponding checked checkboxes) not getting updated properly.

change name attribute of text input elements in following way,

  • name="forbrug[]" name="forbrug[<?php echo $row['jobnr']; ?>]"
  • name="rekvisitioner[]" name="rekvisitioner[<?php echo $row['jobnr']; ?>]"
  • name="reguleringer[]" name="reguleringer[<?php echo $row['jobnr']; ?>]" and
  • name="forecast2[]" name="forecast2[<?php echo $row['jobnr']; ?>]"

and after form submission, update checked rows in following way,

// code $chkcount = count($editcheck); for($i=0; $i < $chkcount; $i++){         $con->query("update jobs set ui_forbrug='".$forbrug[$editcheck[$i]]."', ui_rekvisitioner='".$rekvisitioner[$editcheck[$i]]."', ui_reguleringer='".$reguleringer[$editcheck[$i]]."', ui_forecast2='".$forecast2[$editcheck[$i]]."', edate = now() monthno = $monthno , jobnr = ".$editcheck[$i]); } 

for reference, updated query:

"update jobs  set ui_forbrug='".$forbrug[$editcheck[$i]]."', ui_rekvisitioner='".$rekvisitioner[$editcheck[$i]]."',  ui_reguleringer='".$reguleringer[$editcheck[$i]]."',  ui_forecast2='".$forecast2[$editcheck[$i]]."',  edate = now()  monthno = $monthno , jobnr = ".$editcheck[$i] 

sidenote: learn prepared statement because right query susceptible sql injection attack. see how can prevent sql injection in php.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -