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

时间:2014-07-27 15:32:35

标签: php html mysql

我一直在尝试使用几个复选框从数据库中删除某些行。到目前为止,我已经设法回显了MySQL表的内容,但通过复选框删除行似乎不起作用。

<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);
                }
            }

            ?>

我没有得到任何结果,因为检查没有通过,但我不知道它有什么问题。但查询是正确的,因此问题必须是选中复选框并获取其ID。

2 个答案:

答案 0 :(得分:1)

此处,使用mysqli_代替mysql_

进行了测试和工作

替换为您自己的凭据。

有些事情,你的复选框确实需要在我的评论中提到的指定元素周围的方括号,即name='checkbox[]'  否则您将收到无效的foreach参数错误。

旁注:有一些格式化,但它确实有效。

<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);
            }
        }

?>

mysqli_* with prepared statements使用PDOprepared statements


修改: mysql_

<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 :(得分:0)

你应该做类似的事情(半假):

<form method="post">

    /begin loop/

    echo '<input type="checkbox" name="row[' . $row['ID'] . ']" />';

    /end loop/

    <input type="submit" />

</form>

然后尝试将其放在处理器页面上:

if ( !empty( $_POST ) )
{
    echo '<pre>' . print_r( $_POST, true ) . '</pre>';
    die( 'see? :-)' );
}

。 。 。

好的,这是您的代码,已编辑:

<?php if ( !empty( $_POST ) ) { echo '<pre>'.print_r( $_POST, true ).'</pre>'; die( 'See?' ); ?>
<form method="post">
<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'><input type='checkbox' name='row[$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>"; 
        }

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

        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);
            }
        }

        ?>

</form>

删除查询尚未修复。尝试自己解决: - )