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

时间:2017-08-15 09:00:45

标签: php mysqli

我的表格有很多行。我希望允许用户通过选中他们希望更新的每一行中的复选框来更新选择的行。

该代码可靠地用于单行更新,但是当选择多行时,更新的数据不是来自相关行。即,复选框正常工作,只有带有复选复选框的行才会更新,但如果有多个已检查的行,则值不是来自正确的行(但是来自其他行 - 已检查或未检查 - 在表单中)。

以下是相关的表单元素:

<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>

以下是我处理它的方式:

$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 个答案:

答案 0 :(得分:0)

隐藏输入没有意义,只需复选框输入元素就可以了。这里的问题是,您没有将输入 text 行与相应的复选框输入相关联,这就是为什么正确的行(对应于已选中的复选框)未正确更新的原因

按以下方式更改文本输入元素的名称属性

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

表单提交后,按以下方式更新选中的行,

// 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]);
}

供您参考,这是更新的查询:

"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]

旁注:了解prepared statement因为您的查询现在容易受到SQL注入攻击。另请参阅how you can prevent SQL injection in PHP