Php 在While循环中删除查询

Php 在While循环中删除查询,php,mysql,twitter-bootstrap,mysqli,while-loop,Php,Mysql,Twitter Bootstrap,Mysqli,While Loop,我正在尝试删除while循环中的数据库记录。我在一个带有while循环的表中显示我的用户列表。我有一个按钮,引导模式打开一个模式窗口。在该窗口中,我有提交删除按钮。使用while循环。 问题是,我试图删除这条记录,但它删除的是随机记录。你能检查一下有什么问题吗 已经谢谢你了。 这是我的密码: <table class="table table-hover"> <thead> <tr> <th>Id</th&

我正在尝试删除while循环中的数据库记录。我在一个带有while循环的表中显示我的用户列表。我有一个按钮,引导模式打开一个模式窗口。在该窗口中,我有提交删除按钮。使用while循环。 问题是,我试图删除这条记录,但它删除的是随机记录。你能检查一下有什么问题吗

已经谢谢你了。 这是我的密码:

<table class="table table-hover">
    <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Surname</th>
        <th>Email</th>
        <th>Password</th>
        <th>*</th>
      </tr>
    </thead>
    <tbody>


<?php

    $q = "SELECT * FROM users";
    $r = mysqli_query($dbc,$q);
    while($list = mysqli_fetch_assoc($r)){
        if(isset($_POST['del_submit'])){
            $q = "DELETE FROM users WHERE id = '$list[id]' ";
            $r = mysqli_query($dbc, $q);
                 header('Location: index.php?page=7');
            }

        echo '<tr>';
        echo '<td>'.$list['id'].'</td>';
        echo '<td>'.$list['name'].'</td>';
        echo '<td>'.$list['surname'].'</td>';
        echo '<td>'.$list['email'].'</td>';
        echo '<td>'.$list['password'].'</td>';
        echo '<td><button class="btn btn-danger btn-xs" data-toggle="modal" data-target=".delete'.$list['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button> ';

        echo '</tr><form method="post" action="#">';         
        echo '<div class="modal fade delete'.$list['id'].'">
              <div class="modal-dialog modal-sm" role="document">
                <div class="modal-content">             
                    <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">Delete <strong class="text-primary">'.$list['name'].' ?</strong></h4>
                    </div>

                    <div class="modal-body">
                    <strong class="text-primary">'.$list['name'].' '.$list['surname'].'</strong><br>
                    Are you Sure?
                    </div>
                    <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>

                    <button type="submit" name="del_submit" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
                    </div>
                </div>
              </div>
            </div></form>'; 

    }
?>




    </tbody>
</table>

身份证件
名称
姓
电子邮件
密码
*

创建一个删除页面并链接到它。 首先,您需要使用
action
id
参数向同一页面添加链接

<?php
// replace 
?>
<button type="submit" name="del_submit" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
<?php
// with this: 
?>
<input type='hidden' name='id' value='<?= $list['id']; ?>'>
<input type='hidden' name='action' value='delete'>
<button class="btn btn-danger" type='submit'><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>

删除

我会首先承认你的脚本是不安全和有效的这种方式。为什么不使用Ajax处理删除操作并调用其他脚本?
您必须使用“删除”按钮传递id值,然后改用通过按钮传递的id值。
用于模式“添加值”属性内的“删除”按钮

<button type="submit" name="del_submit" value="'.$list['id'].'" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
如果您重定向的用户的id为已删除元素,只需使用

`标题('Location:index.php?page=$listItemID');而不是标题('Location:index.php?page=7')

完整代码变为

<table class="table table-hover">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Surname</th>
            <th>Email</th>
            <th>Password</th>
            <th>*</th>
        </tr>
    </thead>
    <tbody>


        <?php

        $q = "SELECT * FROM users";
        $r = mysqli_query($dbc,$q);
        while($list = mysqli_fetch_assoc($r)){
            if(isset($_POST['del_submit'])){
                $listItemID = $_POST['del_submit'];
                $q = "DELETE FROM users WHERE id = '$listItemID' ";
                $r = mysqli_query($dbc, $q);
                header('Location: index.php?page=7');
            }

            echo '<tr>';
            echo '<td>'.$list['id'].'</td>';
            echo '<td>'.$list['name'].'</td>';
            echo '<td>'.$list['surname'].'</td>';
            echo '<td>'.$list['email'].'</td>';
            echo '<td>'.$list['password'].'</td>';
            echo '<td><button class="btn btn-danger btn-xs" data-toggle="modal" data-target=".delete'.$list['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button> ';

            echo '</tr><form method="post" action="#">';         
            echo '<div class="modal fade delete'.$list['id'].'">
              <div class="modal-dialog modal-sm" role="document">
                <div class="modal-content">             
                    <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">Delete <strong class="text-primary">'.$list['name'].' ?</strong></h4>
                    </div>

                    <div class="modal-body">
                    <strong class="text-primary">'.$list['name'].' '.$list['surname'].'</strong><br>
                    Are you Sure?
                    </div>
                    <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>

                    <button type="submit" name="del_submit" value="'.$list['id'].'" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
                    </div>
                </div>
              </div>
            </div></form>'; 

        }
        ?>




    </tbody>
</table>

身份证件
名称
姓
电子邮件
密码
*

你检查过错误日志了吗?您假设查询正在运行。在打开
后立即将错误报告添加到文件顶部,我想它是否会删除最后一条记录?将项目删除到select循环中是很奇怪的。使用条件
删除记录,其中id=(int)$\u POST['id']
而不是像
$list['id']
这样的废话-不要忘记在HTML中添加id为的隐藏输入。它会删除列表中的第一个id。例如,第一个id=1秒=2。。。第四:4例如:如果我单击第三个删除按钮,它将删除第一条记录。thnks但是如果您现在可以将其添加到页面顶部,那么我需要在同一页面中修复它。通过
$\u GET
传递要删除的id是一个坏主意。如果robot/crawler发现此url模式并决定对这些链接进行爬网,则表数据将被意外清除。使用
$\u POST
。如果您要费劲地抛出一个
(int)
,那么一个准备好的语句就太过分了。仅供参考,
isset()
可以接收多个参数并保持相同的逻辑。在列名前面加上
`users`.
在只处理一个表时是不必要的。@mickmackusa这样的页面应该受到保护,不受爬虫的影响(需要身份验证),无论如何,我已经更新了我的答案,我删除了整数转换并保留了prepared语句delete查询使用的是未经检查/未初始化的用户提供的数据-这是不好/不安全的做法。编写迭代删除查询是一种糟糕/低效/不合逻辑的做法。如果
$listItemID
是一个整数,则不需要单引号包装。我认为任何人都不应该从这段代码中的建议中学习。输出到屏幕后,您正在调用
header()
if(isset($_POST['del_submit'])){
    $listItemID = $_POST['del_submit'];
    $q = "DELETE FROM users WHERE id = '$listItemID' ";
    $r = mysqli_query($dbc, $q);
        header('Location: index.php?page=7');
}
<table class="table table-hover">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Surname</th>
            <th>Email</th>
            <th>Password</th>
            <th>*</th>
        </tr>
    </thead>
    <tbody>


        <?php

        $q = "SELECT * FROM users";
        $r = mysqli_query($dbc,$q);
        while($list = mysqli_fetch_assoc($r)){
            if(isset($_POST['del_submit'])){
                $listItemID = $_POST['del_submit'];
                $q = "DELETE FROM users WHERE id = '$listItemID' ";
                $r = mysqli_query($dbc, $q);
                header('Location: index.php?page=7');
            }

            echo '<tr>';
            echo '<td>'.$list['id'].'</td>';
            echo '<td>'.$list['name'].'</td>';
            echo '<td>'.$list['surname'].'</td>';
            echo '<td>'.$list['email'].'</td>';
            echo '<td>'.$list['password'].'</td>';
            echo '<td><button class="btn btn-danger btn-xs" data-toggle="modal" data-target=".delete'.$list['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button> ';

            echo '</tr><form method="post" action="#">';         
            echo '<div class="modal fade delete'.$list['id'].'">
              <div class="modal-dialog modal-sm" role="document">
                <div class="modal-content">             
                    <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">Delete <strong class="text-primary">'.$list['name'].' ?</strong></h4>
                    </div>

                    <div class="modal-body">
                    <strong class="text-primary">'.$list['name'].' '.$list['surname'].'</strong><br>
                    Are you Sure?
                    </div>
                    <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>

                    <button type="submit" name="del_submit" value="'.$list['id'].'" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
                    </div>
                </div>
              </div>
            </div></form>'; 

        }
        ?>




    </tbody>
</table>