I'm trying to create a login page with sessions that differentiates between 2 types of users, "Admin" and "Sub" where each type of user will be directed to a different landing page upon a successful login. This is done by comparing username and password entered against the username and password in the database, where a match for both fields will result in a successful login.
The usernames and password entered by the user will be compared against values drawn from different tables which correspond to the differing types of users.
However, i am getting the error Parse error: syntax error, unexpected '{', expecting '(' on line 32. I've done some searching around StackOverflow and the closest answer i could find is from Parse error Unexpected (, expected , in php when assign content file to a static property which doesn't seem to apply to me, from what i can make of it.Most of the questions i've seen on regarding parse errors were due to people forgetting to close their " } " or " ) ". However, i'm getting an error regarding an unexpected " { ", when a " ( " is expected. Why is this so???
My code is as follows:
session_start();
switch ($_POST[‘Button’])
{
case "Login":
include("cxn.inc");
$cxn=mysqli_connect($host,$user,$pass,$db) or die("Can't connect to database");
$query="SELECT * FROM Users WHERE Username='$_POST[Username]'";
$result=mysqli_query($cxn,$query) or die (" Cant find Username");
$num=mysqli_num_rows($result);
if($num>0)//Username is found
{
$query="SELECT * FROM Users WHERE Password='$_POST[Password]'";
$result2=mysqli_query($cxn,$query)or die("Cant find Password");
$num2=mysqli_num_rows($result2);
if($num2>0)//Password is found
{
$_SESSION['Auth']="Yes";
$_SESSION['Type']="Admin";
header("Location:AdminMain.php");
}
else
{
echo"Invalid Username and/or Password";
}
}
elseif//Executes when Username not found in Users table
{ <---This is the line giving me the error
$query="SELECT * FROM SubUsers WHERE Username='$_POST[Username]'";
$result3=mysqli_query($cxn,$query) or die(" Cant find Username");
$num3=mysqli_num_rows($result3);
if($num3>0)//Username is found
{
$query="SELECT * FROM SubUsers WHERE Password='$_POST[Password]'";
$result4=mysqli_query($cxn,$query);
$num4=mysqli_num_rows($result4);
if($num4>0)//Password is found
{
$_SESSION['Auth']="Yes";
$_SESSION['Type']="Sub";
header("Location:SubMain.php");
}
else
{
echo"Cant find Password";
}
}
else
{
echo"Cant find Username";
}
}
else
{
echo"Invalid Username and/or Password entered";
}
include("LoginPage.php");
break;
}
I would appreciate it if anyone could point out my mistake.
Answer
This is because if you use an else if
then you will have to specify a condition.
Example
elseif($num == 0)
{
This will make it goes check if > 0
then if = 0
and else will be < 0
No comments:
Post a Comment