Depending on the responsibilities that have been assigned to them in the database, I need to reroute users to various sites. On the login page, only the username and password are entered. I must retrieve the role from the database, which appears as follows:
username | password | role
xxxxxx xxxxxx admin
xxxxxx xxxxxx trainer
xxxxxx xxxxxx trainer
xxxxxx xxxxxx Line Manager
Here is my code:
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$sql="SELECT * FROM login WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$role = mysqli_fetch_array($result);
if($role == "Admin"){
header("location:index.php");
exit();
}
elseif($role['role'] == "Trainer"){
header("location:index1.php");
exit();
}
elseif($role['role'] == "Line Manager"){
header("location:index2.php");
exit();
}
elseif($role['role'] == "Client"){
header("location:client.php");
exit();
}
}
else {
echo "Wrong Username or Password";
}
?>
Can someone help me with this?