-2

I'm trying to redirect my login into other page if the information are not correct. It seems the else statement is incorrect. please help me! thanks

syntax error, unexpected 'else' (T_ELSE)

<?php
$host="localhost";
$username="root";
$password="";
$dbname="oims2_db";
$tbl_name="administrator_tbl";
$tbl_name2="employee_tbl";
mysql_connect($host,$username,$password) or die("error for connect");
mysql_select_db($dbname) or die("error");

$AdminI = $_POST['AdminI'];
$AdminU = $_POST['AdminU'];
$AdminP = $_POST['AdminP'];

$sql="SELECT * FROM $tbl_name WHERE Admin_ID='$AdminI' and Admin_Username='$AdminU' and Admin_Password='$AdminP'";
$result=mysql_query($sql);
$count= mysql_num_rows($result);

$_SESSION["login_val"] = $count;

if($count==1)
{
echo "login successful " . $_SESSION{"login_val"};
header("location:1home.php");
}

 $mysqli="SELECT * FROM $tbl_name2 WHERE Emp_ID='$AdminI' and Emp_Firstname='$AdminU' and Emp_Lastname ='$AdminP'";

    $result1=mysql_query($mysqli);
$count1= mysql_num_rows($result1);
$_SESSION1["login_val1"] = $count1;


else
{
    header("location:1portal.php");

}

?>
devpro
  • 16,184
  • 3
  • 27
  • 38
  • 1
    Some proper indention of your code would make it **a lot** easier to read. – Qirel Mar 28 '17 at 14:47
  • code b/w the IF and ELSE is an issue.. :) and `mysql` extension is the other one. – devpro Mar 28 '17 at 14:47
  • the "else {" should be directly after "}" of if – Hossam Mar 28 '17 at 14:48
  • 1
    doesn't the else statement have to come right after the if, with no code between them? – Cr1xus Mar 28 '17 at 14:48
  • 1
    Your if block is closed, then there is some code and then your else block decoupled from the if. This will never work. But it is useless to debug this code, because it is that bad security issue that it should never be deployed to the web. – Rene M. Mar 28 '17 at 14:48

1 Answers1

0

The last part of your code should read as follows:

$mysqli="SELECT * FROM $tbl_name2 WHERE Emp_ID='$AdminI' and Emp_Firstname='$AdminU' and Emp_Lastname ='$AdminP'";

$result1=mysql_query($mysqli);
$count1= mysql_num_rows($result1);
$_SESSION1["login_val1"] = $count1;
if($count==1)
    {
    echo "login successful " . $_SESSION["login_val"];
    header("location: 1home.php");
    exit;
    }

    else
    {
        header("location: 1portal.php");
        exit;
    }    


    ?>

An else statement should always follow immediately after an if statement. There should be no code between.

Also I noticed you are still using mysql, which is now depreceated. Use mysqli or PDO instead.

Community
  • 1
  • 1
tyrone 1988
  • 321
  • 2
  • 11
  • So, when you redirect away with your headers, how come you expect the last piece of code to be executed? – Qirel Mar 28 '17 at 15:04
  • My bad, a slight oversight on my part there... – tyrone 1988 Mar 28 '17 at 15:05
  • Most likely that code should be inside the `if`, but there are so many pitholes in the code, we can never be sure, can we ;-) Also, it should be noted that one should always use `exit;` after `header("Location: ..);` calls. – Qirel Mar 28 '17 at 15:06
  • Yes indeed, the code is horrible to look at, however I'll be honest when I first started PHP my code etiquette was just as bad :). I've moved that second query before the IF statement is called so the session can actually be populated. And I agree, exit should be used! – tyrone 1988 Mar 28 '17 at 15:09