I need some help with my login script. I am just trying to self teach php and mysqli and stuck with this so any advice/help would be much appreciated.
The actual part that checks the login details works and directs the user a page if the match is found or back to login page if unsuccessful.
What I want to happen is once the username has been checked and the sessions set use the session 'user' to then find the same user from either the pupil or instructor table depending on the session 'member_type'. The session 'user' is set as the 'user_id' which is the same in the login table and the table the rest of the details are stored in and is the primary key of both tables.
<?php
include_once ("includes/dbconnect.php");
session_start();
$error="";
if ( isset($_POST['btn_signin']) ) {
// username and password received from loginform
$username=mysqli_real_escape_string($conn,$_POST['username']);
$password=mysqli_real_escape_string($conn,$_POST['user_password']);
$sql_checklogin="SELECT * FROM user_logins WHERE username='$username' and password='$password'";
$result=mysqli_query($conn,$sql_checklogin);
$login=mysqli_fetch_array($result,MYSQLI_ASSOC);
$count=mysqli_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1)
{
// Set Sessions
$_SESSION['logged_in']=TRUE;
$_SESSION['user']=$login['user_id'];
$_SESSION['member_type']=$login['reg_type'];
} else {
header("location:login.php");
$error = "Invalid Username or Password!";
}
}
//This part to be replaced with code that checks both tables to find the user details
if ( isset($_SESSION['member_type'])) {
header("Location:mydashboard.php");
}
?>
//The above code works and allows user to login but sends both member types to same page
Just now this works fine but I want to replace the last IF part of the script with something like this (or a better suggestion?).
//if ( isset($_SESSION['member_type'])=='pupil' {
//$sql_findpupil="SELECT * FROM pupils WHERE user_id='$_SESSION['user']'";
//$result=mysqli_query($conn,$sql_findpupil);
//$pupil=mysqli_fetch_array($result,MYSQLI_ASSOC);
Should I be putting another count function at this part?
//header("location:mydashboard.php");
//}elseif ( isset($_SESSION['member_type'])=='instructor' {
//$sql_findinstructor="SELECT * FROM instructors WHERE user_id='$_SESSION['user']'";
//$result=mysqli_query($conn,$sql_findinstructor);
//$instructor=mysqli_fetch_array($result,$MYSLQI_ASSOC);
And here?
//header("location:control_panel.php");
//}
//}
As I said, I am trying to teach myself some new skills at home so have probably made some basic school boy errors here and missed something obvious