我正在尝试检查登录的人是用户还是管理员,我在我的数据库中创建了一个名为'pin'的列,并将值设置为0或1。 如果引脚为0,那么登录的是管理员和用户。 这是我的代码,但它不能与我合作,请帮忙,我是初学者
$query = mysql_query("SELECT ID, password FROM facultymember ".
"WHERE password='$password' AND ID='$ID'", $connection);
$query2 = mysql_query("SELECT pin FROM facultymember ".
"WHERE password='$password' AND ID='$ID'", $connection);
$row2 = mysql_fetch_assoc($query2);
$check = $row2['pin'];
$rows = mysql_num_rows($query);
if ($rows == 1 && $check == 1) {
$_SESSION['login_user']=$ID; // Initializing Session
header("location: homeFM.php"); // Redirecting To Other Page
}
else if($rows == 1 && $check == 0) {
$_SESSION['login_user']=$ID; // Initializing Session
header("location: homeA.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysql_close($connection); // Closing Connection
}
答案 0 :(得分:1)
好的,所以我重新编写了你必须尝试的内容并通过评论来理解它。
$query = mysql_query("SELECT ID, password, pin FROM facultymember ".
"WHERE password='$password' AND ID='$ID'", $connection);
$row = mysql_fetch_assoc($query);
if(false != $row){ // user info exists/correct
$_SESSION['login_user'] = $row['ID'];
if('1' == $row['pin']) { //not admin
header("location: homeFM.php"); // Redirecting To Other Page
die;
} else { //admin
header("location: homeA.php"); // Redirecting To Other Page
die;
}
} else { //login doesn't exist
$error = "Username or Password is invalid";
}
mysql_close($connection); // Closing Connection
这应该对你有用,希望这些评论可以解释它。如果它不起作用,请告诉我,但它应该根据您提供的信息。
有人说......
我希望您的密码经过哈希处理,输入已经过清理,并且您希望使用PDO
或MySQLi
类而不是旧的已弃用的mysql_
函数。
由于需要注意session_start();
来启动会话,而不仅仅是在超全局范围内分配变量。
答案 1 :(得分:0)
Error: There is 1 more closing curly braces '}' found
我猜这是需要删除的最后一个大括号
$query = mysql_query("SELECT ID, password FROM facultymember WHERE password='".$password."' AND ID=".$ID, $connection);
$query2 = mysql_query("SELECT pin FROM facultymember WHERE password='".$password."' AND ID=".$ID, $connection);
您的查询有奇怪的连接