PHP用户级别/管理员面板

时间:2015-10-28 07:40:18

标签: php mysql

我正在为区分会员和管理员的代码工作,但是当我登录时,他只知道我是管理员。我很感激我能得到的一切帮助!

$pdoStatement = $conn->prepare( 'SELECT email FROM tbl_user WHERE user_level = 1' ); 
$pdoStatement->execute(); 
$resultObject = $pdoStatement->fetchAll(); 

if ($resultObject[0] && $_SESSION['loged_in']) {    
  $_SESSION['is_admin'] = true;
  header('Location: index.php?page=admin.php');      
} else{
  $_SESSION['is_admin'] = false;
  $_SESSION['is_member'] = true;
  header('Location: index.php?page=member.php');
}  

这是tbl_user的表结构:

id int(11)  Primarykey
state varchar(255)
f_name varchar(255)
l_name varchar(255)
email varchar(255)
conf_email varchar(255)
password varchar(255)
conf_password varchar(255)
answer varchar(255)
user_level int(11)

下面是我的tbl_user_level

的表结构
id int(11)
name varchar(255)

id 1(name)代表Admin,2代表Member

编辑:代码现在正常,结果在评论中。我很抱歉我的错误我是新来的。对于下一个问题我可能会有所改进。祝你好日子好!

2 个答案:

答案 0 :(得分:1)

$pdoStatement = $conn->prepare(
    'SELECT COUNT(*) FROM tbl_user WHERE user_level = 1 AND email = ?'
); 
$pdoStatement->execute(array($_POST['email'])); 
$resultObject = $pdoStatement->fetchAll();

if($resultObject[0] == 1) { // COUNT(*) == 1 <=> admin user found
    header('Location: index.php?page=admin.php');
} else {
    header('Location: index.php?page=member.php');
}

可替换地:

$pdoStatement = $conn->prepare(
    'SELECT user_level FROM tbl_user WHERE email = ?'
); 
$pdoStatement->execute(array($_POST['email'])); 
$resultObject = $pdoStatement->fetchAll();

if(count($resultObject) === 0) {
    throw new Exception('User not found');
}

if($resultObject[0] == 1) { // user_level == 1
    header('Location: index.php?page=admin.php');
} else {
    header('Location: index.php?page=member.php');
}

答案 1 :(得分:-1)

$pdoStatement = $conn->prepare( 'SELECT * FROM tbl_user WHERE user_level = 1' ); 
$pdoStatement->execute(); 
$resultObject = $pdoStatement->fetchAll(); 

foreach ($resultObject as $data) {
 if ($data['email'] == $_POST['email']) {    
  header('Location: index.php?page=admin.php');      
} else{
  header('Location: index.php?page=member.php');
}
}