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条)

报告相同问题?

悬赏问题

  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥20 win11无法启动 持续蓝屏且系统还原失败,无法开启系统保护
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码