I'm a student and I want to explore more on PHP. I try using functions but I have issues that I dont understand. I want a separated login for the admin and user but always takes me to the first if
statement.
<?php
session_start();
$connect = mysqli_connect('localhost','root','');
if (!$connect){
die ("Unable to connect ".mysqli_connect_error($connect));
exit();
}
if (!mysqli_select_db($connect,'gram')) {
die ("Unable to select ".mysqli_error($connect));
exit();
}
$email = $_POST['email'];
$password = $_POST['password'];
$adminemail = "manager@gramcab.com";
$adminpass = "gram123";
function loginUser(){
global $connect, $email, $password;
$statement = mysqli_prepare($connect, "SELECT * FROM tbl_users WHERE email=? AND password=? LIMIT 1");
mysqli_stmt_bind_param($statement, "ss", $email, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
$count = mysqli_stmt_num_rows($statement);
mysqli_stmt_close($statement);
if ($count < 1){
return true;
}else {
return false;
}
}
function loginAdmin(){
global $connect, $adminemail, $adminpass;
$statement = mysqli_prepare($connect, "SELECT * FROM tbl_gramusers WHERE email=? AND password=? LIMIT 1");
mysqli_stmt_bind_param($statement, "ss", $adminemail, $adminpass);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
$count = mysqli_stmt_num_rows($statement);
mysqli_stmt_close($statement);
if ($count < 1){
return true;
}else {
return false;
}
}
if(loginUser()){
header('Location:../index.html');
}
if (loginAdmin()){
header('Location:../admin.html');
}
?>