2016-12-03 41 views
0

我認爲if語句有問題,因爲當我在上一頁選擇教師選項時,它使我通過學生選項。我的if語句每次都會根據我的選項將它變爲相同的查詢

我的if語句帶我去同一個查詢各當其應該對我的選擇

<?php 

require_once 'config.php'; 
require_once 'databaseclass.php'; 
$br = "<br>"; 
$db = new databaseclass($pdo); 

$email = $_POST["email"]; 
$password = $_POST["password"]; 
$salt = 'itnvv7b96t8ug679ytfj89'; 
$hashedpassword = password_hash($password, PASSWORD_DEFAULT, array(
    'salt' => $salt 
)); 

$option = $_POST["option"]; 
//if statement Correction 
if($option == "student"){ 

    $sql = "SELECT * FROM students WHERE email = '".$email."' AND password = '".$hashedpassword."'"; 
    $db -> query ($sql); 
    $rows = $db->query($sql)->fetchAll(); 
    $count = count($rows); 

     foreach ($rows as $results){ 
      $studentID = $results->studentID; 
      $firstName = $results->firstName; 
      $surname = $results->surname; 
      $email = $results->email; 
     } 

     if ($count == 1){ 
      session_start(); 

      $_SESSION['loggedin'] = TRUE; 
      $_SESSION['studentID'] = $results->studentID; 
      $_SESSION['firstName'] = $results->firstName; 
      $_SESSION['surname'] = $results->surname; 
      $_SESSION['email'] = $results->email; 

      header('Location:studenthomepage.php'); 
     } 

     else { 
      session_start(); 
      session_destroy(); 
      header('Location:login.html'); 
     } 

} 

else{ 

    $sql = "SELECT * FROM `teachers` WHERE `email`='".$email."' AND `password`='".$hashedpassword."'"; 
    $db -> query ($sql); 
    $rows = $db->query($sql)->fetchAll(); 
    $count = count($rows); 

    foreach ($rows as $results){ 
     $teacherID = $results->teacherID; 
     $firstName = $results->firstName; 
     $surname = $results->surname; 
     $email = $results->email; 

    } 

     if ($count == 1){ 
     session_start(); 

     $_SESSION['loggedin'] = TRUE; 
     $_SESSION['teacherID'] = $results->teacherID; 
     $_SESSION['firstName'] = $results->firstName; 
     $_SESSION['surname'] = $results->surname; 
     $_SESSION['email'] = $results->email; 

     header('Location:teacherhomepage.php'); 

     } 

     else { 
     session_start(); 
     session_destroy(); 
     header('Location:login.html'); 
     } 


} 

回答

0

變化有關的時間你的if語句應該是 if($option == "student")而不是if($option = "student")

嘗試。它可能工作。

+0

@DMat是否有效? –

+0

接受解決方案,如果它的工作..謝謝 –

相關問題