在PHP中从管理面板更改用户登录状态


change user login status from admin panel in PHP

我正在研究一个系统,用户可以注册和登录,并看到自己的个人资料& &;有一个主管理员谁可以看到和修改用户登录状态。如果管理员更改了用户状态,则用户无法登录到他的帐户。(阻止用户)。预定义的用户状态是"Y",如果管理员想限制该用户登录,可以将状态更改为"N",这样用户就不能登录了。

我正在学习PHP,我没有正确的技术来做到这一点。

下面是我显示用户表并希望更改状态的代码。

<table border="1" class="pricing-table-wrapper col-sm-4">
    <tr>
        <th>Id</th>
        <th>Username</th>
        <th>Email</th>
        <th>password</th>
        <th>first_name</th>
        <th>last_name</th>
        <th>city</th>
        <th>signup_date</th>
        <th>ac_number</th>
        <th>bank_name</th>
        <th>Status</th>
        <th>Action</th>

    </tr>
    <?php
    //We get the IDs, usernames and emails of users
    $req = mysqli_query($conn, 'select id, username, email, password, first_name, last_name, city, signup_date, ac_number, bank_name, status from users');
    while($dnn = mysqli_fetch_array($req))
    {
    ?>
    <tr>
        <td class="left"><?php echo $dnn['id']; ?></td>
        <td class="left"><?php echo htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8'); ?></td>
        <td class="left"><?php echo htmlentities($dnn['email'], ENT_QUOTES, 'UTF-8'); ?></td>
        <td class="left"><?php echo htmlentities($dnn['password'], ENT_QUOTES, 'UTF-8'); ?></td>
        <td class="left"><?php echo htmlentities($dnn['first_name'], ENT_QUOTES, 'UTF-8'); ?></td>
        <td class="left"><?php echo htmlentities($dnn['last_name'], ENT_QUOTES, 'UTF-8'); ?></td>
        <td class="left"><?php echo htmlentities($dnn['city'], ENT_QUOTES, 'UTF-8'); ?></td>
        <td class="left"><?php echo htmlentities($dnn['signup_date'], ENT_QUOTES, 'UTF-8'); ?></td>
        <td class="left"><?php echo htmlentities($dnn['ac_number'], ENT_QUOTES, 'UTF-8'); ?></td>
        <td class="left"><?php echo htmlentities($dnn['bank_name'], ENT_QUOTES, 'UTF-8'); ?></td>

        <td class="left"><?php echo htmlentities($dnn['status'], ENT_QUOTES, 'UTF-8'); ?></td>
        <td class="left">CHANGE USER STATUS</td>


           <?php
         }
      ?>
    </tr>
    </table>

我试过的代码

 <td>
        <a href='changestatus.php?id=<?=$dnn['id']?>'>
            <img src="a6block.jpg" width="50px" >

        </a>

        <?php 
        include('config.php');
        if(isset($_GET['id']))
        {
        // lookup the current status of user 'id'
        // Update DB with the new status    
        $sql_query="SELECT * FROM users WHERE id=".$_GET['id'];
            $result_set=mysqli_query($conn, $sql_query);
            $fetched_row=mysqli_fetch_array($result_set);

             $sql_query = "UPDATE users SET status='N' WHERE id=".$_GET['id'];
            mysqli_query($conn, $sql_query);

        }
        header("Location: users.php");
        exit;
        // redirect back to the previous page

        ?>

?>

这可能是一个错误的技术,所以我能做什么?

创建一个指向新PHP页面的链接,并传递用户ID .

<td class="left">
    <a href="changestatus.php?id=<?=$dnn['id']?>">
        <?php echo htmlentities($dnn['status'], ENT_QUOTES, 'UTF-8'); ?>
    </a>
</td>

changestatus.php

if(isset($_GET['id'])){
    // lookup the current status of user 'id'
    // Update DB with the new status    
}
// redirect back to the previous page
header("Location: /previouspage.php");
exit;
为了防止页面重新加载,您总是可以使用AJAX.

这没有经过测试,但是对于AJAX方法,您将不得不做这样的事情…

<td class="left">
    <a href="#" class='changeStatus' id='<?php echo $dnn['id'] ?>'>
        <?php echo htmlentities($dnn['status'], ENT_QUOTES, 'UTF-8'); ?>
    </a>
</td>
$(document).ready(function () {
    $('.changeStatus').click(function(e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: 'changestatus.php',
            data: this,
        })
        .done(function() {
            //success
        })
        .fail(function() {
            //failure
        });
    });
 });

别忘了.. ..AJAX将更改DB值,但显示仍将显示原始值。因此,您必须在AJAX调用的'success'部分更新显示