如何在php和mysql中选中时删除多个复选框?

时间:2014-02-19 05:28:45

标签: php mysql

我非常困惑并且搞砸了我应该如何一次删除所选的复选框。有人可以帮忙吗?我已经尝试了许多删除代码并对其进行了评论,因为它已经失败了。所以请建议!!!!      以下代码是为“DELETE ALL”按钮编写的!

/*if(isset($_POST['delete']))
        {
            mysql_query("DELETE FROM dealer_tbl");
        }

“DELETE SELECT”按钮上的一些路径

if($_REQUEST['Delete'] != '')
        {
            if(!empty($_REQUEST['checkboxstatus'])) {
            $checked_values = $_REQUEST['checkboxstatus'];
            foreach($checked_values as $val) {
                $sqldel = "DELETE from dealer_tbl WHERE id = '$val'";
               mysql_query($sqldel);

            }
        }
    } 

OR

 if($delete){
 for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $dealer_tbl WHERE id='$del_id'";
$result = mysql_query($sql);
}*/
?>

DELETE ALL的按钮类型

<button type="submit" id="delete" name="delete" value="delete"  data-loading-text="Loading...">Delete All</button>

DELETE SELECTED的按钮类型

    <button type="submit" id="deletes" name="delete" value="delete"  data-loading-text="Loading..." onClick="delete_check.php">Delete Selected</button>

表格如下,

<table class="table table-bordered">
                <tr>
                    <th>Title</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Mobile</th>
                    <th>Phone</th>
                </tr>
                <?php
                    $res=mysql_query("SELECT * FROM dealer_tbl");
                    while($dealer_row=mysql_fetch_array($res))
                    {
                    ?>
                        <tr>

                            <td><?php echo $dealer_row['title']; ?></td>
                            <td><?php echo $dealer_row['firstname']; ?></td>
                            <td><?php echo $dealer_row['lastname']; ?></td>
                            <td><?php echo $dealer_row['email']; ?></td>
                            <td><?php echo $dealer_row['phone']; ?></td>
                            <td align="center"><a href='dealer_edit.php?did=<?=$dealer_row['uid']?>'>Edit</a> | <a href='dealer_delete.php?did=<?=$dealer_row['uid']?>'>Delete</a></td>
                            <td><input type="checkbox" name="checkbox[]" id="checkbox[]" value="<?php echo $dealer_row['uid']; ?>"  />
    </td> 
                        </tr>
                    <?php
                    }
                ?>
            </table>
    </div>

3 个答案:

答案 0 :(得分:2)

简单的解决方案,[自己动手模式]

  1. 将您的复选框命名为name="checkbox[$row[id]]"
  2. 然后当提交这些复选框时,您将获得一个数组,其中Key是要删除的ID。
  3. 提取数组键,将它们以逗号分隔列表进行内嵌。
  4. 运行SQL查询delete from table where id in (<yourimplodedkeys>)
  5. 亲自尝试,过上快乐。

答案 1 :(得分:0)

也将表格放在<form>标签内。在这种情况下,复选框的参数将接收。

答案 2 :(得分:0)

你的正确答案是:

<?php
$delete=$_POST['delete'];
$chkbox=$_POST['checkbox'];
if($delete){
  for($i=0;$i<count($chkbox);$i++){
    $del_id = $chkbox[$i];
    $sql = "DELETE FROM $dealer_tbl WHERE id='$del_id'";
    $result = mysql_query($sql);
  }
 ?>