2016-12-29 134 views
0

環顧四周後,我設法創建了一個簡單的登錄。我可以用我的用戶名和密碼登錄,但不幸的是,登錄驗證不起作用。當我輸入錯誤的密碼或將文本框留空並單擊登錄時,表單只會被重置。它不顯示錯誤消息。我確定我錯過了一些東西,但我不知道它是什麼。PDO:登錄驗證

請幫幫我。謝謝:)

這裏是我的代碼:

的index.php

<?php 
session_start(); 

$conn = new PDO ('mysql:host=localhost;dbname=test','root',''); 

if (isset($_POST['login'])){ 
    $UserId = $_POST['UserId']; 
    $UserPwd = $_POST['UserPwd']; 


if(empty($UserId) && empty($UserPwd)) { 
    $message = "Username/Password con't be empty"; 
    } 
     else 
      { 
      $sql = "SELECT UserId, UserPwd FROM user WHERE UserId=? AND UserPwd=? "; 
      $query = $conn->prepare($sql); 
      $query->execute(array($UserId,$UserPwd)); 

     if($query->rowCount() >= 1) { 
      $_SESSION['UserId'] = $UserId; 
      header("location: login.php"); 
      } 
      else 
       { 
       $message = "Username/Password is wrong"; 
      } 
     } 
    } 
?> 

<html> 
    <head><title>Login</title></head> 
    <body> 
     <form method="post" name="login"> 
      <input type="text" name="UserId"> 
      <input type="text" name="UserPwd"> 
      <input type="submit" name="login" value="Login"> 
     </form> 
    </body> 
</html> 

回答

0

斐伊川你只是忘記您的呼應$message。這就是爲什麼你沒有收到任何消息。

$message = ""; 

<html> 
     <head><title>Login</title></head> 
     <body> 
      <span><?php echo $message?></span>// echo your message here 
      <form method="post" name="login"> 
       <input type="text" name="UserId"> 
       <input type="text" name="UserPwd"> 
       <input type="submit" name="login" value="Login"> 
      </form> 
     </body> 
    </html> 
+0

頁的前頭現在的工作宣告你的消息。謝謝:) @ user1234 –