[英]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.