-1

I'm trying to design a multi user login system but for some reason there seem to be a problem with else. Getting the following error:

Error: Parse error: syntax error, unexpected token "else" in C:\Users\brand\OneDrive\Desktop\XAMAPP\htdocs\Water Tower 2000\index.php on line 175

I'm not sure what to do. I just simply want to found the error that has to do with else.

require_once 'connection.php';

session_start ();

if(isset ($_SESSION["admin_login"]))

{
    header ("location:admin/admin_home.php");
}

if(isset ($_SESSION["parent_login"]))

{
    header ("location:parent/parent_home.php");
}

if(isset ($_SESSION["swimmer_login"]))
{
    header ("location:swimmer/swimmer_home.php");
}


if (isset ($_REQUEST['btn_login'])) {

    $email = $_REQUEST ["txt_email"];

    $password = $_REQUEST ["txt_password"];

    $email = $_REQUEST ["txt_role"];

if(empty($email)){

    $errorMsg[]="please enter yout water tower email";

}

else if(empty($password)){

    $errorMsg[]="please enter yout water tower email";
}

else if(empty($role)){

    $errorMsg[]="please enter yout water tower email";

}


else if($email AND $password AND $role){

try{

$select_stmt=$db->prepare("SELECT email,password,role FROM masterlogin WHERE email=:uemail

AND password=:upassword AND role=:urole");

$select_stmt->bindParam(":uemail",$email);

$select_stmt->bindParam(":upassword",$password);

$select_stmt->bindParam(":urole",$role);

$select_stmt->excute();

while ($row=$select_stmt->fetch(PDO::FETCH_ASSOC)){

    $dbemail =$row["email"];

    $dbpassword =$row["password"];

    $dbrole =$row["role"];

}

if($email!=null AND $password!=null AND $role!=null){

    if($select_stmt->rowCount()>0){

        if ($email!==$dbemail AND $password==$dbpassword AND $role==$dbrole){

            switch($dbrole) {

                case "admin":

                 $_SESSION ["admin_login"]=$email;
                 $loginMsg="Admin...Your in Water Tower...";
                 header("refresh:3;admin/admin_home.php");
                 break;

                 case "parent":
                 $_SESSION["parent_login"]=$email;  
                 $loginMsg="Parent...Welcome To Water Tower...";
                 header("refresh:3;parent/parent_home.php");
                 break;

                 case "swimmer":
                    $_SESSION ["swimmer_login"]=$email;
                    $loginMsg="Fellow swimmer...Your in Water Tower...";
                    header("refresh:3;swimmer/swimmer_home.php");
                    break;

                 default:
            $errorMsg[]="Sorry but either the email/password/role is wrong";
                   
                  }

                }
              
              else {
       
           $errorMsg="Sorry but either the email/password/role is wrong";
                 
                 }
            }
             
              else { 

            $errorMsg="Sorry but either the email/password/role is wrong";
                 
                 }

              }
            
               else {
                     
         $errorMsg="Sorry but either the email/password/role is wrong";  

                    }

                  }  

                else {
                        
             $errorMsg="Sorry but either the email/password/role is wrong";

                    }

                 }
 
                

               catch (PDOException $e){

                $e->getMassage();

               }

            }

            else {

            $errorMsg="Sorry but either the email/password/role is wrong";

            }

        }

  ?>
  • 1
    This would be way easier to solve if you used some sensible code indention and formatting. – M. Eriksson May 06 '22 at 10:58
  • 1
    Your first step will be to format your code with consistent indentation and consistent use of curly braces to denote code blocks. Making your code human-readable will help you, as a human, read it. Once you've done this, the error will become clear. Somewhere you have an `else` block that isn't following an `if` block. – David May 06 '22 at 10:58
  • Please not that `&&` and `AND` has slightly different behaviour: https://stackoverflow.com/questions/2803321/and-vs-as-operator – Justinas May 06 '22 at 10:59
  • 1
    `require_once _'connection.php';` - that's first issue, remove `_` – Justinas May 06 '22 at 11:00
  • 2
    **Warning!** Never store passwords in plain text! You should only store password hashes generated using [password_hash()](https://www.php.net/manual/en/function.password-hash.php) and to verify a password againts a hash, use [password_verify()](https://www.php.net/manual/en/function.password-verify.php). – M. Eriksson May 06 '22 at 11:04
  • After formatting the code, it seems like your `catch` for your `try` is in the wrong place so it's `try { ... } else { ... }`. – M. Eriksson May 06 '22 at 11:06
  • Everyone I just formatted the code but so that's it's easier to understand. Any new insights about the code. – Brandon Ontama May 06 '22 at 11:25
  • @BrandonOntama: You *changed* it, but you didn't *fix* it. The indentation and curly braces are all still entirely random and unreadable. A decent IDE can help you format your code and maintain consistency. In a pinch you can also make use of [online tools](https://www.tutorialspoint.com/online_php_formatter.htm) to format code for you. When you do so, you'll find that the second-to-last `else` block in the code above isn't attached to an `if`. It's attached to a `try`. Which is invalid. You might also try re-structuring your logic to avoid many nested `if` blocks like that. – David May 06 '22 at 11:36
  • The else error was gone but now I have an new error. Instead of else being unexpected it's 'catch'. – Brandon Ontama May 06 '22 at 11:53
  • 1
    @BrandonOntama: Which you would resolve in the same way. By consistently formatting your code and observing the mis-aligned code blocks. As well as by re-structuring your logic so you're not confusing yourself with it. – David May 06 '22 at 11:59
  • 1
    `I just formatted the code but so that's it's easier to understand`...no, it's a lot harder to understand this way, because lots of bits are misaligned and you can't easily see which blocks of code are within which others. I've no idea how you can imagine that this version is better, it hurts my eyes...no-one sane writes their code in this poorly-formatted way - it just makes your life 100 times more difficult. There's a reason why good IDEs auto-indent things for you. Justinas took the time to format it properly for you, and then you went and vandalised it again. – ADyson May 06 '22 at 12:01
  • P.S. As an aside, why are you asking the user to provide their role at login? It's not important for authentication...the username should be unique anyway without that, surely? And normally applications are designed with the possibility for a user to have more than one role, anyway – ADyson May 06 '22 at 12:02

0 Answers0