删除循环中的查询

时间:2017-12-06 14:14:02

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

我正在尝试删除while循环中的数据库记录。我在带有while循环的表中显示我的用户列表。我有一个按钮,bootstrap模式打开一个模态窗口。在那个窗口中,我提交了删除按钮。随着循环。 问题是,我正在尝试删除此记录,但删除随机记录。你能检查一下是否有问题?

谢谢。 这是我的代码:

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

2 个答案:

答案 0 :(得分: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)';
}

答案 1 :(得分:-1)

我首先会承认你的脚本不是那么安全有效。为什么不用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,只需使用

`标题(&#39;位置:index.php?page = $ listItemID&#39;);而不是标题(&#39;位置:index.php?page = 7&#39;);

完整代码变为

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