使用mysql数据库中的排名

时间:2016-11-22 14:43:21

标签: php mysql select

如果用户具有排名' 1'我怎样才能实现排名功能?如果他有' 0'他有管理员权限。他是普通用户

我知道我必须使用像

这样的东西
$sql = "
SELECT * 
  from users 
 WHERE username LIKE '{$username}' 
   AND password LIKE '{$password}'  
 LIMIT 1";

但不确定如何正确使用

我的数据库看起来像

username password rank
tom      tom1     0
john     john1    1  
<html>
<head>
    <title>SRC Centr</title>
</head>
<body>
<h1><center>SRC Centr</h1>
<?php
if (!isset($_POST['submit'])){
?>
<!-- The HTML login form -->
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
        <center>Username: <input type="text" name="username" required /><br /><center>
        <center>Password: <input type="password" name="password" required /><br /><center>

        <input type="submit" name="submit" value="Login" />
    </form>
<?php
} else {
    require_once("db_const.php");
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    # check connection
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }

    $username = $_POST['username'];
    $password = $_POST['password'];
    $rank = $_POST['rank'];

    $sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}'  LIMIT 1";
    $result = $mysqli->query($sql);
    if (!$result->num_rows == 1) {
        echo "<center><h3>Incorrect login</h3></center>";
    } else {
        if ( $rank == "0"){
    header("location: app.php");
 }
else if ( $rank == "1"){
header("location: index.php");
}





}
}
?>      
</body>
</html>

谢谢:)

2 个答案:

答案 0 :(得分:0)

伪代码:

if((SELECT rank FROM users WHERE username = {username} and password = {password}) == 1) {
    set rank of user to admin
} 

这是你要的吗?

答案 1 :(得分:0)

我不确切地知道你想要什么,但你可以试试这个

$sql ="SELECT username, rank from users 
        WHERE 
           username ='".$_post["username"]."' AND 
           password ='".$_post["password"]."'";
$result = $mysqli->query($sql);
if( $result == 1) {
    if($user = mysqli_fetch_array($result))
        if($user["rank"] == 1){
            #Do something
        } else {
            #DO somthing
        }
    }
} else { 
    die ("Error");
}

如果您需要其他内容,请告诉我