我创建了一个具有多个用户的登录名。我已连接 phpMyAdmin 数据库并且单用户登录工作正常。使用login.php代码,它不会重定向到预期的页面。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();
?>
这是寄存器表。并且正在从该数据中获取登录过程的数据:
最佳答案
正如 @Mikey 指出的,您将 mysql_query
与 mysqli
的语句准备和绑定(bind)混合在一起。而且您还滥用了查询中的引号。
假设您的 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
那你应该没问题。
关于php将多个用户登录后重定向到多个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40505269/