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 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码