简体   繁体   English

当我选择多个复选框进行删除时...只删除最后一个复选框! 意味着它可以从数据库中删除1个数据

[英]when I am selecting multiple checkbox for deletion…only last one checkbox is deleted ! means it can delete 1 data from a database

Below code !Gives me check boxes and a delete button, In input tag all check box have same name (check)!! 下面的代码!给我复选框和删除按钮,在输入标签中所有复选框都有相同的名称(检查)!! There check box can be retreive from database with id . 可以从具有id的数据库中检索复选框。

Problem Is:: when I am selecting multiple checkbox for deletion...only last one checkbox is deleted ! 问题是::当我选择多个复选框进行删除时......只删除最后一个复选框! means it can delete 1 data from a database. 意味着它可以从数据库中删除1个数据。

url like -> http://localhost/demo/delete.php?check=10&check=13&check=14&submit=Delete url like - > http://localhost/demo/delete.php?check = 10&check = 13&check = 14&submit = Delete

I need while I am selecting a checkbox more than 1 checkbox ,check box datas is deleted from database ! 当我选中一个复选框超过1复选框时我需要,复选框数据从数据库中删除! Any one help me to overcome this problem thanks 任何人帮我解决这个问题谢谢

  1. index.php 的index.php

     <?php $sql = mysql_connect('localhost', 'root', ''); mysql_select_db('database_section', $sql); ?> <form name="checkbox" method="get" action="delete.php"> <table> <tr> <?php $sql = "select * from data"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { ?> <td><input type="checkbox" name="check" value="<?php echo $row['id']?>"><?php echo $row['data'];?> </td> <?php } ?> <tr> <td><input type="submit" name="submit" value="Delete"></td> </tr> </table> </form> 
  2. Now, In delete.php..code below... 现在,在下面的delete.php..code中...

     <?php $sql = mysql_connect('localhost', 'root', ''); mysql_select_db('database_section', $sql); if ($_REQUEST['submit']) { $abc = $_GET['check']; $sql = "Delete from data where id=$abc"; $result = mysql_query($sql) or die(mysql_error()); if (isset($result)) { echo "data deleted"; } else { echo "not possible"; } } ?> 

Use check box as an array holder. 使用复选框作为数组持有者。 name it as check[] to hold all selected values. 将其命名为check []以保存所有选定的值。 And on post you will get the selected array list. 在帖子上,您将获得所选的数组列表。

Now your $abc will be a array, use foreach in delete.php to get the checked ids. 现在你的$ abc将是一个数组,在delete.php中使用foreach来获取已检查的id。

[...]
while ($row = mysql_fetch_array($result))
{
        ?>
        <td>
            <input type="checkbox" name="check[]" value="<?php echo $row['id']?>"><?php echo $row['data']; ?>
        </td>
    <?php
}
?>
[...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM