我创建了一个包含多个用户的登录信息。我已连接phpMyAdmin数据库和单用户登录正常工作。使用login.php代码,它不会被重定向到预期的page.dbconnect.php工作。
的login.php
<?php
require('dbconnect.php');
if(isset($_POST['uname']) && isset($_POST['pw'])){
$email=$_POST['uname'];
$pw=$_POST['pw'];
if ($stmt = mysql_query("SELECT role FROM register WHERE 'email'= ? and 'pw'= ? ")) {
/* bind parameters for username and password */
$stmt->bind_param('ss', $email, $pw);
/* execute query */
$stmt->execute();
// If result matched $myusername and $mypassword, table row must be 1 row
if ($stmt->affected_rows == 1) {
// bind the result to a variable
$stmt->bind_result($role);
$stmt->fetch();
switch( $role ){
case 'admin':
header("location:admin.php");
exit();
case 'trackcoordinator':
header("location:trackco.php");
exit();
case 'reviewer':
header("location:reviewer.php");
exit();
case 'author':
header("location:sub.php");
exit();
default:
echo "Wrong Username or Password";
}
}
$stmt->close();
}
}
//$db->close();
?>
这是注册表。从登录过程中获取数据:
答案 0 :(得分:1)
正如@Mikey指出你正在混合mysql_query
和mysqli
的声明准备和绑定。而且你也在滥用查询中的引号。
让我们假设您的dbconnect.php看起来像这样
<?php
$db= new mysqli('localhost', 'my_user', 'my_password', 'world');
?>
然后你的login.php第10行(if语句)应该像
if ($stmt = $db->prepare("SELECT role FROM register WHERE email = ? and pw = ? ")) { // You can either use backquotes (`email`) or no quotes
然后你应该没事。
答案 1 :(得分:0)
尝试将exit()
替换为break()
,我也注意到您在=
声明中使用了==
而不是if()
。尝试改变它。
编辑: 进行多个用户重定向的另一种更好的方法是在数据库表中有一个额外的列,称为access_level(或类似的东西),int值如1,2,3 ...,然后检查访问级别当前用户并根据用户的访问级别重定向到不同的网址