根据复选框从数据库中删除一行
Posted
技术标签:
【中文标题】根据复选框从数据库中删除一行【英文标题】:Deleting a row from the database based on checkbox 【发布时间】:2015-11-13 15:29:50 【问题描述】:我正在尝试删除包含问题文本和类型的整行。在删除的情况下,该功能可以正常工作。但是,它总是删除添加的最后一行,而不是检查的行。有什么建议吗?
这是我的表结构:
case 'Addquiz':
$sql = "SELECT id,questiontext,type FROM questioninfo ORDER BY type DESC ";
$result = mysqli_query($con,$sql);
$selectedtable = "<form method='post' action=''>\n";
$selectedtable .= "<table class='sortable'>\n<tr><th>Select</th><th>Question</th><th>Type</th></tr>\n";
while($row = mysqli_fetch_assoc($result))
$rowID = $row['id'];
$text = $row['questiontext'];
$type = $row['type'];
$selectedtable .= "<tr><td><input type='checkbox' name='delete' value='Delete' style='margin:20px;'></td><td><input type='text' name='QuestionText[$rowID]' value='$text' style=' width:600px; text-align:left;'></td><td><select name='Type[$rowID]' style='margin:10px; height:35px'><option selected='selected'></option><option value='$type'>Performace</option><option value='$type'>Loyalty</option></select></td></tr>\n";
$selectedtable .= "</table>\n";
$selectedtable .= "<input type='submit' name='addquestion' value='Add Question' style='width:140px; height:30px; text-align:center; padding:0px;'>\n";
$selectedtable .= "<input type='submit' name='submit' value='Update' style='width:80px; height:30px; text-align:center; padding:0px;'>\n";
$selectedtable .= "<input type='submit' name='del' value='Delete' style='width:80px; height:30px; text-align:center; padding:0px;'>\n";
$selectedtable .= "</form>\n";
if(isset($_POST['submit']))
foreach($_POST['QuestionText'] as $rowID => $text)
$sql = "UPDATE questioninfo SET questiontext = '$text', type = '$type' WHERE id = '$rowID'";
mysqli_query($con,$sql);
if(isset($_POST['addquestion']))
$sql="INSERT INTO `questioninfo` (`ID`) VALUES (NULL)";
mysqli_query($con,$sql);
if(isset($_POST['del']))
$sql="DELETE FROM questioninfo WHERE id = '$rowID'";
mysqli_query($con,$sql);
break;
【问题讨论】:
$rowID 将始终获取最后一行 id,因为它是在 while 循环之后执行的。 @ravenanigam 关于如何获得所需结果的任何建议? 【参考方案1】:<?php
if(isset($_POST['submit']))
$checkbox=$_POST['chk'];
for($i=0;$i<count($checkbox);$i++)
$id = $checkbox[$i];
$sql1 = "DELETE FROM manufacturer WHERE id ='$id' ";
mysql_query($sql1) or die(mysql_error());
?>
<form name="unit" method="post">
<table cellpadding="0" cellspacing="0" >
<?php
$selUnit = "select id, name from manufacturer order by id desc limit 20";
$rsUnit = mysql_query($selUnit);
$rows = mysql_num_rows($rsUnit);
if($rows>0)
while($arrUnit = mysql_fetch_array($rsUnit))
?>
<tr >
<td >
<input type="checkbox" name="chk[]" id="chk[]" value="<?=$arrUnit["id"]?>">
</td>
<td align="left">
<?=$arrUnit["name"]?>
</td>
</tr>
<?php
?>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="submit" >
</td>
</tr>
</table>
</form>
【讨论】:
现在不使用 javascript 检查。使用提交按钮而不是 javascript 函数。【参考方案2】:这是一个简单的例子,展示了你想做的事情的可能方式:
<?php $values = array( 1, 2, 3 ); ?>
<form method="post">
<?php foreach($values as $value)
echo "<input type='checkbox' name='delete$value' value='Delete'/>";
?>
<input type="submit" name="submit"/>
</form>
<?php
if(isset($_POST['submit']))
foreach($values as $value)
if(isset($_POST["delete$value"]) && $_POST["delete$value"])
echo "delete$value is true";
else
echo "delete$value is false";
echo "<br/>";
?>
在您的代码中可以这样做:
...
$selectedtable .= "<tr><td><input type='checkbox' name='delete$rowID' value='Delete' style='margin:20px;'></td><td><input type='text' name='QuestionText[$rowID]' value='$text' style=' width:600px; text-align:left;'></td><td><select name='Type[$rowID]' style='margin:10px; height:35px'><option selected='selected'></option><option value='$type'>Performace</option><option value='$type'>Loyalty</option></select></td></tr>\n";
...
if(isset($_POST['del']))
while($row = mysqli_fetch_assoc($result))
$rowID = $row['id'];
if(isset($_POST["delete$rowID"]) && $_POST["delete$rowID"])
$sql="DELETE FROM questioninfo WHERE id = '$rowID'";
mysqli_query($con,$sql);
但是,这种方式需要在删除时对数据库进行第二次查询以获取所有行 ID。正如 Shailesh 在他的解决方案中暗示的那样,最佳方法是从表单中发布一组复选框。
【讨论】:
以上是关于根据复选框从数据库中删除一行的主要内容,如果未能解决你的问题,请参考以下文章