【问题标题】:Delete Query Inside While Loop在 While 循环中删除查询
【发布时间】:2018-05-20 10:58:05
【问题描述】:

我正在尝试在 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>

【问题讨论】:

  • 您检查过错误日志吗?您正在假设查询正在运行。在您打开 &lt;?php 标记 error_reporting(E_ALL); ini_set('display_errors', 1); 后立即在文件顶部添加错误报告
  • 我认为它删除了最后一条记录,或者没有?将项目删除到选择循环中很奇怪。使用条件 where id = (int)$_POST['id'] 删除记录,而不是像 $list['id'] 这样的废话 - 不要忘记在 HTML 中添加带有 ID 的隐藏输入。
  • 它删除列表中的第一个 id。例如第一个 id = 1 第二个 =2 ...第四个:4 例如:如果我点击第三个删除按钮,它会删除第一条记录。

标签: php mysql twitter-bootstrap mysqli while-loop


【解决方案1】:

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

<?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>

将此添加到页面顶部

<?php

if(isset($_POST['id'], $_POST['action']) && $_POST['action'] === 'delete')              
{ 
   $id = $_POST['id'];
   $query = 'DELETE FROM `users` WHERE `id` = ?';
   $db = new Mysqli('localhost','user','password','database');
   $stmt   = $db->prepare($query);
   $stmt->bind_param('i',$id);
   $stmt->execute();
   $stmt->close();
   $db->close();
   echo 'Deleted! (if exists)';
}

【讨论】:

  • 谢谢,但我需要在同一页面中进行此操作
  • 如果您现在可以将其添加到页面顶部,则已修复
  • 通过$_GET 传递要删除的id 是个坏主意。如果机器人/爬虫发现此 url 模式并决定爬取这些链接,则您的表数据将被意外清除。使用$_POST。如果您要费心使用(int),那么准备好的语句就大材小用了。仅供参考,isset() 可以接收多个参数并保持相同的逻辑。当只处理一张表时,不需要在列名前面加上`users`.
  • @mickmackusa 这样的页面应该受到爬虫保护(需要身份验证),无论如何我已经更新了我的答案,我删除了整数转换并保留了准备好的语句
【解决方案2】:

我将首先承认您的脚本以这种方式不安全和高效。为什么不用 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 并使用 id。它应该工作

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

如果您将用户重定向到带有已删除元素的 id 的位置,只需使用

`header('位置:index.php?page=$listItemID');而不是 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>

【讨论】:

  • 删除查询使用未经检查/未经处理的用户提供的数据 - 这是不好/不安全的做法。编写迭代的 DELETE 查询是不好/低效/不合逻辑的做法。如果$listItemID 是整数,则不需要用单引号包裹。我认为任何人都不应该从这段代码中的建议中学习。输出到屏幕后,您正在调用header()
猜你喜欢
  • 2013-03-11
  • 2017-09-12
  • 2022-07-21
  • 2013-01-15
  • 1970-01-01
  • 2018-03-27
  • 2019-09-18
  • 2013-07-01
  • 1970-01-01
相关资源
最近更新 更多