[英]php to redirect user based on “role” using mysqli
<?php
$host = 'localhost';
$username = 'admin2';
$password = 'vaptek';
$db_name = 'vaportek_db';
// Connect to server and select database.
$db = new mysqli($host, $username, $password, $db_name );
if( $db->connect_errno ){
die('Unable to connect to database [' . $db->connect_error . ']');
}
// username and password sent from form
$myusername=$_POST['userId'];
$mypassword=$_POST['userPw'];
$stmt = $db->prepare("SELECT role FROM users WHERE `username`=? and `password`=?");
/* bind parameters for username and password */
$stmt->bind_param('ss', $myusername, $mypassword);
/* execute query */
$stmt->execute();
// If result matched $myusername and $mypassword, table row must be 1 row
if ($test == 1) {
// bind the result to a variable
$stmt->bind_result($role);
$stmt->fetch_object()->$role;
switch( $role ){
case 'director':
header("location: director.php");
break;
case 'customer':
header("location: cust.php");
break;
case 'production manager':
header("location: prodmanager.php");
break;
case 'account admin':
header("location: accountadmin.php");
exit();
default:
echo "Wrong staff ID or password";
}
$stmt->close();
}
$db->close();
?>
当我运行代码时,它只是给我一个空白页,不会从login.php继续进行
我已经做过echo $ test = $ stmt-> affected_rows; 它显示为-1,我对php不太熟悉,也不太了解它在哪里出错。
...
/* execute query */
$stmt->execute();
$res = $stmt->get_result();
// If result matched $myusername and $mypassword, table row must be 1 row
if ($res->num_rows == 1) {
...
if($stmt->execute()){
//Gets the results and assigns it to $result
$result = $stmt->get_result();
//runs a while loop so that the role can be pulled out and assigned to $role for the switch
while($row = $result->fetch_assoc()){
$role = $row['role'];
}
// If result matched $myuser
最终解决了! 感谢您的帮助,使我朝着正确的方向前进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.