简体   繁体   中英

How to put a if statement to link to another page if a sql result variable is set

I am trying to redirect a user to another page if the CHPassword column is "yes" Currently if there user name and pass is valid I send them to dashboard.php if it is not I send them to index.php. Now I'm trying to check the database and if the CHPassword is yes. force them to change their password. (redirect to another page. Otherwise let them proceed to the dashboard. Here is the code currently.

    // SQL query to fetch information of registerd users and finds user match.
$sql = "SELECT * FROM Employee where Password='$EPassword' AND UserName='$UserName'";
    $result = mysqli_query($con, $sql) or die(mysqli_error($con));
    $User = mysqli_fetch_array($result);
$row = mysqli_num_rows($result);
if ($row == 1) 
{
$_SESSION["FName"] = $User['FName'];
$_SESSION["LName"] = $User['LName'];
$_SESSION["AccessLvl"] = $User['AccessLvl']; // Initializing Session header

header("location: Dashboard.php");
}
else
{
$_SESSION["error"] = "UserName Or Password is Incorrect. Please try Again";
header("location: index.php"); // Redirecting To Other Page

}
mysqli_close($con); // Closing Connection
}
}

All help would be great. Sorry guys still learning. Thanks in advance.

If your CHPassword has record like 'yes' /

if ($row == 1) 
{
  if($User["CHPassword"]=='yes'){
     header("location: changepassword.php");
  }
  else{ 
    $_SESSION["FName"] = $User['FName'];
    $_SESSION["LName"] = $User['LName'];
    $_SESSION["AccessLvl"] = $User['AccessLvl']; 
    header("location: Dashboard.php");
  }
}
else{
   $_SESSION["error"] ="UserName Or Password is Incorrect.";
   header("location: index.php"); // Redirecting To Other Page
}

I am not sure if this is what you are looking for. But if you are trying to check the value for CHPassword that is returned from the query, and check if it is set to Yes, and redirect based on that, then this should work.

$sql = "SELECT * FROM Employee where Password='$EPassword' AND UserName='$UserName'"; $result = mysqli_query($con, $sql) or die(mysqli_error($con)); $User = mysqli_fetch_array($result); $row = mysqli_num_rows($result);

if ($row == 1) 
{
    if ( $User['CHPassword'] == "Yes" )
    {
        //place header code here
    }
    $_SESSION["FName"] = $User['FName'];
    $_SESSION["LName"] = $User['LName'];
    $_SESSION["AccessLvl"] = $User['AccessLvl']; // Initializing Session header

    header("location: Dashboard.php");
}
else
{
    $_SESSION["error"] = "UserName Or Password is Incorrect. Please try Again";
    header("location: index.php"); // Redirecting To Other Page

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM