I currently have two tables, Users and Customers both with a username and password columns. Users are created on the admin end and customers can register to the website however, there is only one login page and I need to be able to extract the username and password from each of the different tables and redirect the user to the correct pages respectively. Here's what we have so far;
if (isset($_POST['submitted'])){
unset($_POST['submitted']);
//sanitise those inputs.
$username = $db->quote($_POST['username']);
//select the user with the correct name
$q = "select users.*, customers.* from users INNER JOIN customers on customers.username=users.username WHERE users.username = $username || customers.username = $username";
$rows = $db->query($q);
//if the password matches then redirect
foreach ($rows as $row) {
if ($row["password"] == $_POST['password']) { //password matches
//set some session variables for using later
$_SESSION['username'] = $_POST['username'];
$_SESSION['userid'] = $row['userid'];
$_SESSION['customerid'] = $row['customerid'];
$_SESSION['role'] = $row['role'];
//redirect to either the staff or user home page based on the login
if ($row["role"] == "customer"){
header( 'Location: customerHome.php' );
} else if ($row["role"] == "staff" || $row["role"] == "admin" ) {
header( 'Location: staffHome.php' );
}
else {
header( 'Location: customerHome.php' );
}
}
}
//if the redirect doesn't happen then there was an error with the password, so display this and the form
//echo '<h1> There was a problem with your user name or password </h1>';
echo"<script language='javascript'>
window.onload = function(){
var divs = document.getElementById('error');
divs.innerHTML = '<p style = \"color:#FF0000\">There was a problem with your username or password, please try again!</p>';
}
</script>";
}
I think the issue is with the SQL statement which selects the fields as it just keeps throwing/ jumping to the error message. Thanks for the help.