dongmen1860 2017-08-15 09:00
浏览 101
已采纳

使用复选框从表单更新选定的行 - 添加错误行的数据

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]);
 }
  • 写回答

1条回答 默认 最新

  • douzi6060 2017-08-15 09:28
    关注

    There's no point having hidden inputs, just checkbox input elements would be fine. Here the problem is, you're not associating input text rows with the corresponding checkbox inputs, that's why the correct rows(corresponding to the checked checkboxes) are not getting updated properly.

    Change the name attribute of your text input elements in the following way,

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

    And after form submission, update the checked rows in the following way,

    // your 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() WHERE monthno = $monthno AND jobnr = ".$editCheck[$i]);
    }
    

    For your reference, this is the 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() 
    WHERE monthno = $monthno AND jobnr = ".$editCheck[$i]
    

    Sidenote: Learn about prepared statement because right now your query is susceptible to SQL injection attack. Also see how you can prevent SQL injection in PHP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100