2016-11-09 137 views
0

我已經創建了多個用戶登錄後。我已連接phpMyAdmin數據庫,單用戶登錄正常工作。與login.php代碼它沒有重定向到預期的page.dbconnect.php正在工作。PHP重定向多個用戶的多個頁面登錄

的login.php

<?php 

require('dbconnect.php'); 

if(isset($_POST['uname']) && isset($_POST['pw'])){ 

    $email=$_POST['uname']; 
    $pw=$_POST['pw']; 

if ($stmt = mysql_query("SELECT role FROM register WHERE 'email'= ? and 'pw'= ? ")) { 
    /* bind parameters for username and password */ 
    $stmt->bind_param('ss', $email, $pw); 


    /* execute query */ 
    $stmt->execute(); 

    // If result matched $myusername and $mypassword, table row must be 1 row 
    if ($stmt->affected_rows == 1) { 
     // bind the result to a variable 
     $stmt->bind_result($role); 
     $stmt->fetch(); 


     switch($role){ 

      case 'admin': 
       header("location:admin.php"); 
       exit(); 

      case 'trackcoordinator': 
       header("location:trackco.php"); 
       exit(); 

      case 'reviewer': 
       header("location:reviewer.php"); 
       exit(); 

      case 'author': 
       header("location:sub.php"); 
       exit(); 

      default: 
       echo "Wrong Username or Password"; 
     } 

    } 

    $stmt->close(); 
} 
} 

//$db->close(); 

?> 

這是寄存器表。而數據從這一對登錄過程獲取:

enter image description here

+0

是登錄信息是否正確?它看起來像你用純文本保存你的密碼。並且不使用MySQL的使用庫MySQLi或PDO –

+0

登錄信息是正確的。當這段代碼運行時,沒有任何錯誤msr。它重新加載空白的頁面 – clay

+0

echo $角色你會得到什麼? –

回答

0

嘗試用break()更換exit(),還我注意到您使用=代替==if()聲明。試着改變它。

編輯: 另一個更好的方法來做多個用戶重定向是在數據庫表中有一個額外的列,名爲access_level(或類似的東西),int值爲1,2,3 ...,然後檢查當前用戶的訪問級別和重定向到根據不同的URL的用戶的訪問級別

1

由於@Mikey指出你混合mysql_querymysqli的報表編制和約束力。而且你也在濫用查詢中的引號。

讓我們假設你的dbconnect.php看起來像這樣

<?php 
$db= new mysqli('localhost', 'my_user', 'my_password', 'world'); 
?> 

那麼你的login.php線10(if語句)應該是這樣

if ($stmt = $db->prepare("SELECT role FROM register WHERE email = ? and pw = ? ")) { // You can either use backquotes (`email`) or no quotes 

那麼你應該罰款。