通过复选框删除数据库条目 [英] Deleting database entries through checkboxes

查看:83
本文介绍了通过复选框删除数据库条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < table class =ts> 
< tr>
< th class =tg-031estyle ='width:1px'> ID< / th>
< th class =tg-031e> IP地址< / th>
< th class =tg-031e>原因< / 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']。 >< /形式>< / 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> 中;
回声< / tr>;
}

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

if(isset($ _ POST ['delete'])&& isset($ _ POST ['checkbox'])){
foreach($ _ POST ['checkbox'] as $ id){
$ id =(int)$ id;
$ delete =删除禁止的WHERE ID = $ id;
mysql_query($ delete);
}
}

?>

我没有收到任何结果,因为支票没有通过,但我不知道什么是错的用它。查询是正确的,所以问题必须是选择复选框并获得它们的ID。

解决方案

在这里,使用 mysqli _ 代替 mysql _



替换你的自己的凭证。

有几件事,你的复选框确实需要在命名元素周围的方括号,正如我在我的评论中提到的那样,即 name ='checkbox []'
否则您将收到无效的 foreach 参数错误。


$

 < table class = TS > 
< tr>
< th class =tg-031estyle ='width:1px'> ID< / th>
< th class =tg-031e> IP地址< / th>
< th class =tg-031e>原因< / 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 =删除禁止的WHERE ID = $ id;
mysqli_query($ con,$ delete);
}
}

?>






使用 mysqli _ * strong> PDO 准备好的语句




编辑: mysql _ 版本

 < table class =ts> 
< tr>
< th class =tg-031estyle ='width:1px'> ID< / th>
< th class =tg-031e> IP地址< / th>
< th class =tg-031e>原因< / th>
< / tr>

<?php

包含'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 =删除禁止的WHERE ID = $ id;
mysql_query($ delete);
}
}

?>


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.

解决方案

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

?>

这篇关于通过复选框删除数据库条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆