情况:问题列表将从数据库中读取并显示在表格中。我可以添加新行或删除我想要的行。将提交的数据将使用数组存储。更新和插入完美无缺,如下所示。
问题:如果用户删除了表格中的某些行,我该如何删除数据库中的这些数据。
抱歉语法/英语不好。谢谢你的帮助!
Html代码:
<table class="msgBox" id="simulator2">
<tr>
<th></th>
<th class="msgSubject">QUESTION</th>
</tr>
<?php
$qry="SELECT ques_item, ques_id FROM log_question WHERE eva_id = '$action'";
$result=mysqli_query($con, $qry);
$count=mysqli_num_rows($result);
$i=1;
while($rows=mysqli_fetch_array($result)){
$eQuesID = $rows['ques_id'];
$eQues = $rows['ques_item'];
?>
<tr class="table-row">
<td><input type="checkbox" name="chk" /></td>
<td class="dataBox"><input type="text" name="eQues[]" value="<?php echo $eQues; ?>" /><input type="hidden" name="eQuesID[]" value="<?php echo $eQuesID; ?>" /></td>
</tr>
<?php $i++;
} $tempCount = $i-1;
?>
</table>
<br />
<center>
<input type="button" value="Add Row" onclick="addRow('simulator2')" />
<input type="button" value="Delete Row" onclick="deleteRow('simulator2')" />
</center>
Php代码:
$a = 0;
foreach (array_combine($eQues, $eQuesID) as $eQuest => $eQuestID){
if ($a < $tempCount){ // Update Question
$sql="UPDATE log_question SET ques_item='$eQuest' WHERE ques_id='$eQuestID'";
$test=mysqli_query($con, $sql);
//Check whether the query was successful or not
if(!$test) {
echo("Error description: " . mysqli_error($con));
exit();
}
} else { //Insert New Question
$newsql="INSERT INTO log_question VALUES('', '$eID', '$eQuest')";
$newtest=mysqli_query($con, $newsql);
//Check whether the query was successful or not
if(!$newtest){
echo("Error description: " . mysqli_error($con));
exit();
}
} $a++;
}