0

I am trying to login into admin panel but getting parse error.

Parse error:  syntax error, unexpected '{', expecting '(' 

Actually i have used condition if user role is admin then it should open one page and if user role is equal to employee it should open that page.

But it is getting this error.If am trying like this then

<?php session_start();
include '../includes/db.php';
if(isset($_POST['submit_login'])){
    if(!empty($_POST['user_name']) && !empty($_POST['password'])){
        $get_user_name = mysqli_real_escape_string($conn,$_POST['user_name']);
        $get_password = mysqli_real_escape_string($conn,$_POST['password']);
        $sql = "SELECT * FROM users WHERE username = '$get_user_name' AND user_password = '$get_password' AND user_role='admin'";
        if($result = mysqli_query($conn,$sql)){
            while($rows = mysqli_fetch_assoc($result)){
                if(mysqli_num_rows($result) == 1){
                    $_SESSION['user'] = $get_user_name;
                    $_SESSION['password'] = $get_password;
                    $_SESSION['user_role'] = $rows['user_role'];
                    header('Location:../admin/index.php');
                }
          elseif{
          header('Location:../admin/employeeindex.php');
              }
                else {
                    header('Location:../index.php?login_error=wrong');
                }
            }               
        }

        else {
            header('Location: ../index.php?login_error=query_error');
        }
    } else {
        header('Location:../index.php?login_error=empty');
    }
}else {
}
 ?>
  • 1
    Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Nico Haase Jun 20 '18 at 20:40

4 Answers4

1

You are missing condition in elseif block which is why throwing syntax error

 elseif (condition missing here ){
      header('Location:../admin/employeeindex.php');
 }

Please find more about condition syntax here http://php.net/manual/en/control-structures.elseif.php

0
elseif(mysqli_num_rows($result) != 1){
      header('Location:../admin/employeeindex.php');
}

use this code and it will work .

  • First tell me are you getting the parse error still? – Sankha Chowdhury Jun 20 '18 at 21:03
  • No its not getting – developer d Jun 20 '18 at 21:11
  • Ok thanks , now tell me what you actually looking for . Do you want to pass admin to admin page and employee to employee to employee page ? What is the role of an employee in your database ? – Sankha Chowdhury Jun 20 '18 at 21:13
  • admin will have all the privileges like edit,edit and add but the employee can only se the list he can't edit add or delete – developer d Jun 20 '18 at 21:19
  • I mean what is the value of the field user_role in your database for employee and what is your actual requirement? – Sankha Chowdhury Jun 20 '18 at 21:22
  • in database there is a column for user_role where we insert admin or employee.While registration there is an option as if they are employee or admin.while login if the user name and password are matched and user role is equal to admin then it should go to admin index page else if user role is equal to employee then it should go to employee page – developer d Jun 20 '18 at 21:29
0

I agree with the previous answer, the condition is missing in elseif.

elseif (condition missing here ){ header('Location:../admin/employeeindex.php'); }
Vulcan
  • 3
  • 2
0
$sql = "SELECT * FROM users WHERE username = '$get_user_name' AND user_password = '$get_password'" ;
if($result = mysqli_query($conn,$sql)){
        while($rows = mysqli_fetch_assoc($result)){
            if(mysqli_num_rows($result) == 1){
                $_SESSION['user'] = $get_user_name;
                $_SESSION['password'] = $get_password;
                $_SESSION['user_role'] = $rows['user_role'];
                if($_SESSION['user_role'] === 'admin'){
                    header('Location:../admin/index.php');
                }
                else{
                    header('Location:../admin/employeeindex.php');
                }
             }
             else{
                 header('Location:../index.php?login_error=wrong');
             }
        }
}

Use the above code and it will run successfully.