2016-12-29 7 views
0

ここを見回して、簡単なログインを作成することができました。ユーザーIDとパスワードでログインできますが、残念ながらログインの検証は機能しません。間違ったパスワードを入力したり、テキストフィールドを空白にしてログインをクリックすると、フォームはリセットされます。エラーメッセージは表示されません。私は何かが不足していると確信していますが、私はそれが何であるかの手がかりはありません。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 –

関連する問題