dongzhan9100 2014-07-27 15:32
浏览 73
已采纳

通过复选框删除数据库条目

I have been trying to delete certain rows from the database using a few checkboxes. So far I've managed to echo out the content of the MySQL table but deleting rows through the checkboxes doesn't seem to work.

<table class="ts">
        <tr>
            <th class="tg-031e" style='width:1px'>ID</th> 
            <th class="tg-031e">IP address</th>
            <th class="tg-031e">Date added</th>
            <th class="tg-031e">Reason</th>
        </tr>

        <?php include 'connect.php';

            $SQL = "SELECT `ID`, `IPaddress`, `DateAdded`, `Reason` FROM `banned`";
            $exec = mysql_query($SQL, $connection);

            while ($row = mysql_fetch_array($exec)){
                    echo "<tr class='tg-031h'>";
                    echo "<td class='tg-031e'><form method='post'><input type='checkbox' name='checkbox' value=" . $row['ID'] . "></form></td>";
                    echo "<td class='tg-031e'>" . $row['IPaddress'] . "</td>";
                    echo "<td class='tg-031e'>" . $row['DateAdded'] . "</td>";
                    echo "<td class='tg-031e'>" . $row['Reason'] . "</td>";
                    echo "</tr>"; 
            }

            echo "</table><form method='post'><input name='delete' type='submit' value='Delete'></form>";

            if (isset($_POST['delete']) && isset($_POST['checkbox'])) {
                foreach($_POST['checkbox'] as $id){
                    $id = (int)$id;
                    $delete = "DELETE FROM banned WHERE ID = $id"; 
                    mysql_query($delete);
                }
            }

            ?>

I don't get any result as the check does not pass but I don't know what's wrong with it. The query is correct though, so the issue must be with selecting the checkboxes and getting their ID.

  • 写回答

2条回答 默认 最新

  • doufei2355 2014-07-27 16:39
    关注

    Here, it's tested and working while using mysqli_ instead of mysql_

    Replace with your own credentials.

    A few things, your checkbox did need square brackets around the named element as I mentioned in my comment(s), i.e. name='checkbox[]' otherwise you would receive an invalid foreach argument error.

    Sidenote: There stands to do a bit of formatting, but it works.

    <table class="ts">
            <tr>
                <th class="tg-031e" style='width:1px'>ID</th> 
                <th class="tg-031e">IP address</th>
                <th class="tg-031e">Date added</th>
                <th class="tg-031e">Reason</th>
            </tr>
    
    <?php
    
    $DB_HOST = "xxx";
    $DB_NAME = "xxx";
    $DB_USER = "xxx";
    $DB_PASS = "xxx";
    
    $con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
    if($con->connect_errno > 0) {
      die('Connection failed [' . $con->connect_error . ']');
    }
    
    $SQL = "SELECT `ID`, `IPaddress`, `DateAdded`, `Reason` FROM `banned`";
    $exec = mysqli_query($con,$SQL);
    
    echo "<form method='post'>";
    
            while ($row = mysqli_fetch_array($exec)){
                    echo "<tr class='tg-031h'>";
                    echo "<td class='tg-031e'><input type='checkbox' name='checkbox[]' value='" . $row[ID] . "'></td>";
    
                    echo "<td class='tg-031e'>" . $row['IPaddress'] . "</td>";
                    echo "<td class='tg-031e'>" . $row['DateAdded'] . "</td>";
                    echo "<td class='tg-031e'>" . $row['Reason'] . "</td>";
            }
    
    echo "</tr></table>"; 
    
            echo "<input name='delete' type='submit' value='Delete'></form>";
    
            if (isset($_POST['delete']) && isset($_POST['checkbox'])) {
                foreach($_POST['checkbox'] as $id){
                    $id = (int)$id;
    
                    $delete = "DELETE FROM banned WHERE ID = $id"; 
                    mysqli_query($con,$delete);
                }
            }
    
    ?>
    

    Do use mysqli_* with prepared statements, or PDO with prepared statements for this.


    Edit: mysql_ version

    <table class="ts">
            <tr>
                <th class="tg-031e" style='width:1px'>ID</th> 
                <th class="tg-031e">IP address</th>
                <th class="tg-031e">Date added</th>
                <th class="tg-031e">Reason</th>
            </tr>
    
    <?php
    
    include 'connect.php';
    
    $SQL = "SELECT `ID`, `IPaddress`, `DateAdded`, `Reason` FROM `banned`";
    $exec = mysql_query($SQL, $connection);
    
    echo "<form method='post'>";
    
            while ($row = mysql_fetch_array($exec)){
                    echo "<tr class='tg-031h'>";
                    echo "<td class='tg-031e'><input type='checkbox' name='checkbox[]' value='" . $row[ID] . "'></td>";
    
                    echo "<td class='tg-031e'>" . $row['IPaddress'] . "</td>";
                    echo "<td class='tg-031e'>" . $row['DateAdded'] . "</td>";
                    echo "<td class='tg-031e'>" . $row['Reason'] . "</td>";
            }
    
    echo "</tr></table>"; 
    
            echo "<input name='delete' type='submit' value='Delete'></form>";
    
            if (isset($_POST['delete']) && isset($_POST['checkbox'])) {
                foreach($_POST['checkbox'] as $id){
                    $id = (int)$id;
    
                    $delete = "DELETE FROM banned WHERE ID = $id"; 
                    mysql_query($delete);
                }
            }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题