根据复选框从数据库中删除行

时间:2015-08-20 06:02:31

标签: php mysqli phpmyadmin

我正在尝试删除包含questiontext和type的整行。在删除的情况下,该功能正如所希望的那样工作。但是它总是删除添加的最后一行,而不是检查的那一行。有什么建议吗?

这是我的表结构: enter image description here

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;

2 个答案:

答案 0 :(得分: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 width="100%" 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 width="54%" align="left">
                <?=$arrUnit["name"]?>
            </td>   
        </tr>
        <?php
    }
}
?>
    <tr>
        <td colspan="2" align="center">
            <input type="submit" name="submit" value="submit" >
        </td>
    </tr>
</table>

</form>

答案 1 :(得分:0)

这是一个简单的例子,说明了你想做的事情的可能方式:

<?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在其解决方案中暗示的表单中发布一系列复选框。