dota220141003 2014-06-02 00:40
浏览 412

PHP / MySQL - 检查用户是常规用户还是管理员

I'm trying to check if a user is a regular user or an admin.

In my database, the last item is "usertype" and it is a 0 or 1.

0 for regular user and 1 for admin user.

For some reason this is not working. It always tells me I am a "regular user." Which I know cannot be true. I did set usertype as an int to 1.

Here is my admin.php page

<?php
include_once '../includes/db_connect.php';
include_once '../includes/functions.php';

sec_session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Dashboard</title>
    <link rel="stylesheet" href="style.css" />
</head>
<body>


    <?php
        // Create connection
        $con = mysqli_connect(HOST, USER, PASSWORD, DATABASE);

        // Check connection
        if (mysqli_connect_errno()) {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        $result = mysqli_query($con,"SELECT * FROM members");

        $row = mysqli_fetch_row($result);
    ?>




    <?php
        if ($row["usertype"] == 1) 
        {
            echo "You're an admin!";
        } 

        else 
        {
            echo "You're a regular user.";
        }
    ?>

</body>
</html>
  • 写回答

3条回答 默认 最新

  • doutao5419 2014-06-02 00:46
    关注

    Do you have more than one user in your database? Because "SELECT * FROM members" isn't selecting any specific member.

    You should really be supplying a username to select from the database like so "SELECT * FROM members WHERE username = '$username'"

    Otherwise, check what you're storing usertype as (varchar/int/etc) and make sure you're comparing correctly.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?