2016-05-13 33 views
0

我正在嘗試開發的網站中使用登錄模塊,實際上它不是典型的登錄模塊。在我的登錄表單中,如果您是什麼類型的用戶,可以選擇一個下拉框。在那裏顯示「教職員工」,「部門負責人」和「主管」。每個用戶都有不同的主頁。這裏是我的login.php代碼:根據用戶類型在不同頁面上重定向的登錄

<?php 
session_start(); // Starting Session 
$error=''; // Variable To Store Error Message 

if (isset($_POST['login'])) { 
    if (empty($_POST['username']) || empty($_POST['password'])) { 
      $error = "Username or Password is invalid"; 
    }else{ 
     // Define $username and $password 
     //$user_type=$_POST['user_type']; 
     $username=$_POST['username']; 
     $password=$_POST['password']; 
     // Establishing Connection with Server by passing server_name, user_id and password as a parameter 
     $connection = mysql_connect("localhost", "root", ""); 
     // To protect MySQL injection for Security purpose 
     //$user_type = stripcslashes($user_type); 
     $username = stripslashes($username); 
     $password = stripslashes($password); 
     //$user_type = mysql_escape_string($user_type); 
     $username = mysql_real_escape_string($username); 
     $password = mysql_real_escape_string($password); 
     // Selecting Database 
     $db = mysql_select_db("cpecs", $connection); 
     // SQL query to fetch information of registerd users and finds user match. 
     $query = mysql_query("select * from tblaccounts where password='$password' AND username='$username'", $connection); 
     $rows = mysql_num_rows($query); 

     if ($rows == 1) { 
      $_SESSION['login_user']=$username; // Initializing Session 
       if ($_POST['user_type']= Faculty) { 
        header("location: userhome.php"); //Redirecting to Faculty Home Page 
       }else if ($_POST['user_type']= Department) { 
        header("location: adminhome.php"); //Redirecting to Department Head Home Page 
       }else if ($_POST['user_type']= Supervisor) { 
        header("location: supervisorhome.php"); // Redirecting to Supervisor Home Page 
       }else{ 
        $error = "Oops! It seems that you are not an authorized user."; 
       } 
     } 
    mysql_close($connection); // Closing Connection 
    } 
} 

?> 

我也只是叫usertypetbluser_type我的數據庫中的值,顯示它在下拉框中。這裏是我的代碼

<?php 
    include('connection.php'); 
     $sql = "SELECT * FROM tbluser_type"; 
     $result = mysqli_query($conn, $sql); 
     $option_usertype=""; 

     while($row = mysqli_fetch_array($result)){   
      $option_usertype= $option_usertype."<option> $row[1] </option>"; 
     } 

?> 
+0

$ option_usertype = 「」; while $($ row = mysqli_fetch_array($ result)){ $ option_usertype。=「」; } –

+0

請檢查我的答案 –

回答

1

請修改代碼

if ($rows == 1) { 
      $_SESSION['login_user']=$username; // Initializing Session 
       if ($_POST['user_type'] == 'Faculty') { 
        header("location: userhome.php"); //Redirecting to Faculty Home Page 
       }else if ($_POST['user_type'] == 'Department') { 
        header("location: adminhome.php"); //Redirecting to Department Head Home Page 
       }else if ($_POST['user_type'] == 'Supervisor') { 
        header("location: supervisorhome.php"); // Redirecting to Supervisor Home Page 
       }else{ 
        $error = "Oops! It seems that you are not an authorized user."; 
       } 
     } 

你的錯誤

if ($_POST['user_type']=Faculty) { 

if ($_POST['user_type'] == 'Faculty') { 

獲取用戶類型

$option_usertype=""; 
    while($row = mysqli_fetch_array($result)){ 
     $option_usertype .= "<option> ".$row['user_type']." </option>"; 
    } 

$行[ 'USER_TYPE']到你的數據庫字段名

+0

好的。謝謝。即時通訊將嘗試它。 :) – newbie

+0

@newbie請檢查你獲得$ _POST ['user_type'] ?? –

+0

您還需要編碼'退出';在'header(...);'之後'' –

相關問題