2011-12-01 56 views
0

這是我的第一頁,這將是我的登錄頁面。在他們的用戶名,用戶類型,點擊登錄,然後broswer應該帶他們去的login.php的Index.html

<html> 
<head> 
<title>Login</title> 
</head> 
<body > 

<br><br><br><br><h2 align=center>Quiz Taker</h2> 
<br><br><br> 
<p align=center></p> 

<br> 
    <form method="post" action="login.php"> 
     <table class="login" cellpadding=10 cellspacing=0 align="center" bgcolor="#cccccc" border="3"> 
     <tr> 
      <td> 
      <input type="radio" name="userType" value="Student"/> Student 
      <input type="radio" name="userType" value="Teacher"/> Teacher 
      <td> 
      Username:</td><td><input type="text" name="username" size=10> 
      </td> 
     </tr> 
     <tr> 
      <td colspan=2 align="center"> 
      <input type="submit" name="submit" value=" Log In "> 
      </td> 
     </tr> 
    </table> 
    </form> 


</body> 
</html> 

當用戶點擊提交就會去的login.php,看起來像這樣:

<?php 

include_once("DatabaseConnection.php"); 

$stmt=$DBH->prepare("SELECT * FROM QuizUser where Name=?") 
$stmt->bindValue(1, $_POST['username']); 
$stmt->execute(); 
if ($row=$stmt->fetch()) 
    { 
     $ID=$row['ID']; 
    } 
    else 
    { 

     $stmt=$DBH->prepare("INSERT INTO QuizUser(Name) VALUES(?)") 
     $stmt->bindValue(1, $_POST['username']); 
     $stmt->execute(); 
     $ID=$DBH->lastInsertId(); 
    } 
    $_SESSION['userID']= $ID; 
    $_SESSION['userName']= $_POST['username']; 
    $_SESSION['userType']=$_POST['userType']; 

    if($_POST['userType'] == "Student") 
    { 
     header($string["Location:student.php"]); 
    } 
    else 
    { 
     header($string["Location:teacher.php"]);  
    } 

    ?> 

哪一個不工作,我不明白爲什麼。它只是彈出一個解析錯誤:語法錯誤,意外的T_VARIABLE在.... myfile ....在第一次嘗試和使用$ _POST變量行。順便說一下,我正在login.php頂部導入的DataBaseConnection.php中開始我的會話。

+0

你的問題是不是真的與你的問題。你的PHP腳本的語法有問題。您需要發佈確切的錯誤消息。 – six8

+2

您在'$ stmt = $ DBH-> prepare(「SELECT * FROM QuizUser where Name =?」)後面缺少分號' – six8

+0

謝謝,我補充說,但仍然沒有解決任何問題。 – Tombo890

回答

1

header功能缺失分號不正確地寫入。

header($string["Location:student.php"]); 

應該

header("Location: student.php"); 

同樣,

header($string["Location:teacher.php"]); 

應該

header("Location: teacher.php"); 
+0

修復它。我現在正在重定向。謝謝!!!! – Tombo890

+0

@ user1005658不客氣。 – mc10

2

您在

$stmt=$DBH->prepare("SELECT * FROM QuizUser where Name=?") 

$stmt=$DBH->prepare("INSERT INTO QuizUser(Name) VALUES(?)") 
+0

真棒,以便修復解析錯誤。但是現在它還沒有采用任何頭文件。這是我的理解,那些將您重定向到「位置:whever.php」 – Tombo890

+0

當我去提供的鏈接http://atr.eng.utah.edu/~kline/ps10/login.php,在錯誤消息中,它表示「完整性約束違規:1048列名'不能爲空'」>這意味着您在表「QuizUser」中有一個名爲「Name」的列,並且您已將該字段設置爲「不可空」。如果表中沒有強制要求具有「名稱」值,請將該字段設置爲可空。 –

+0

默認情況下名稱爲空,所以它應該除空 – Tombo890

1

這裏

include_once("DatabaseConnection.php"); 

你最好使用

require_once("DatabaseConnection.php");