[英]How to separate admin and users on query?
我试图让用户在称为books的表中创建书籍详细信息。 但是,有两种类型的用户,即admin和users。 在数据库的用户表中,将Admin保存为type = 1,将用户保存为type = 0,为boolean。
用户必须登录系统以更新账目。 用户的详细信息在COOKIES中另存为用户名。
因此,我想命令仅允许管理员上载与COOKIES用户名相关的图书表并检查用户名是否与admin有关的计算机,如果用户名是user,则不要创建它。
用户数
id | name | type
1 | abc | 1
2 | xyz | 0
我正在尝试这样做
$_COOKIE['username'];
$result = 'SELECT type FROM users WHERE type = 1';
$res = mysql_query($result);
if ($res){
//let admin to enter the details
} else {
//you are not an admin,
}
您的SQL查询将始终返回1。
$username = $_COOKIE['username'];
$res = mysql_query("SELECT type FROM users WHERE name='$username'");
然后检查类型是1还是0。
$row = $mysql_fetch_assoc($res);
if ($row['type'] == 1){
//You are an admin
} else {
//You are not an admin
}
使用Cookie或会话都没关系。 您需要将某种类型的用户信息(无论是用户的ID还是用户名)传递到SQL查询中,以检查该特定用户的凭据。
现在,假设您有一个名为upload.php的php文件,该文件可以处理书籍上载。 现在,在文件中检查会话变量$_SESSION["type"]
的值是否为1,即,登录用户是管理员。 另外,我建议不要使用mysql_ *语法,因为它已被弃用。 看看mysqli 。 我已经使用mysqli写下了答案。
//This code goes in upload.php file
$mysqli = mysqli_connect(HOST, USER, PASS, DBNAME);
$username = $_COOKIE["username"];
$stmt = $mysqli->prepare("SELECT type FROM users WHERE username=?");
if($stmt){
$stmt->bind_param("s", $username);
$stmt->execute;
$stmt->bind_result($type);
$stmt->fetch();
$stmt->close();
}
//Now check for type
if( $type == 1){
//Add upload code here
}
else{
//Not admin access denied
}
$mysqli->close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.