2016-04-26 161 views
0

這是我從教程中獲得的代碼,當我嘗試登錄時,它不工作!它說:PHP登錄表單錯誤

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\index.php on line 13

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\index.php on line 16

<?php 
     include("config.php"); 
     session_start(); 

     if($_SERVER["REQUEST_METHOD"] == "POST") { 
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['username']); 
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

      $sql = "SELECT * FROM tbl_users WHERE username = '$myusername' and password = '$mypassword'"; 
      $result = mysqli_query($db,$sql); 
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC); 
      $active = $row['active']; 

      $count = mysqli_num_rows($result); 

      // If result matched $myusername and $mypassword, table row must be 1 row 

      if($count == 1) { 
      session_register("myusername"); 
      $_SESSION['login_user'] = $myusername; 

      header("location: welcome.php"); 
      }else { 
      $error = "Your Login Name or Password is invalid"; 
      } 
     } 
    ?> 

任何幫助將是非常讚賞! 在此先感謝。

用「.username。」更改用戶名後...這是代碼!

<?php 
    include("config.php"); 
    session_start(); 

    if($_SERVER["REQUEST_METHOD"] == "POST") { 
     // username and password sent from form 

     $myusername = mysqli_real_escape_string($db,$_POST['username']); 
     $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

     $sql = "SELECT * FROM tbl_users WHERE username = '".$myusername."' and password = '".$mypassword."'"; 
     $result = mysqli_query($db,$sql); 
     $row = mysqli_fetch_array($result,MYSQLI_ASSOC); 
     $active = $row['active']; 

     $count = mysqli_num_rows($result); 

     // If result matched $myusername and $mypassword, table row must be 1 row 

     if($count == 1) { 
     session_register("myusername"); 
     $_SESSION['login_user'] = $myusername; 

     header("location: welcome.php"); 
     }else { 
     $error = "Your Login Name or Password is invalid"; 
     } 
    } 
?> 

<html> 
<head><title>Login Page PHP Script</title></head> 
<body> 
    <div align="center"> 
     <div style="width:300px; border: solid 1px #006D9C; " align="left"> 
      <?php 
       if(isset($errMsg)){ 
        echo '<div style="color:#FF0000;text-align:center;font-size:12px;">'.$errMsg.'</div>'; 
       } 
      ?> 
      <div style="background-color:#006D9C; color:#FFFFFF; padding:3px;"><b>Login</b></div> 
      <div style="margin:30px"> 
       <form action="" method="post"> 
        <label>Username :</label><input type="text" name="username" class="box"/><br /><br /> 
        <label>Password :</label><input type="password" name="password" class="box" /><br/><br /> 
        <input type="submit" name='submit' value="Submit" class='submit'/><br /> 
       </form> 
      </div> 
     </div> 
    </div> 
</body> 
</html> 
+0

當我這樣做,我得到這個......「解析錯誤:語法錯誤,在C意外的文件結尾:\ XAMPP \ htdocs中\ login.php中上線141」 –

+0

而這個:' username ='「。$ myusername。」'AND password ='「。$ mypassword。」''? – Jer

+0

@MichealHarker這不是一個問題現在..我想要的是一些工作代碼登錄 –

回答

0

您的查詢出現錯誤,這就是爲什麼mysqli_fetch_array()返回false而不是對象的原因。

請嘗試以下操作以查看問題所在。

if (!$result) { 
    printf("Error: %s\n", mysqli_error($db)); 
    exit(); 
} 

從您的代碼然而,查詢確實應該(提供用戶名和密碼不爲空)

$sql = "SELECT * FROM tbl_users WHERE username = '" . $myusername . "' and password = '" . $mypassword . "'"; 
+0

Thanks @Rehan ..我輸入的數據庫名稱不正確..但是..still我有2個以上的錯誤 注意:未定義的索引:在C:\ xampp \ htdocs \ index.php上在線14 致命錯誤:未捕獲的錯誤:調用C:\ xampp \ htdocs \ index.php中的未定義函數session_register():第25行中的C:\ xampp \ htdocs \ index.php引發堆棧跟蹤:#0 {main} –

+0

謝謝多!你的回答幫助我發現連接到錯誤的數據庫。 –

+0

歡迎:) –

0

mysqli_real_escape_string()只能包含一個參數 - 字符串逃脫。

$myusername = mysqli_real_escape_string($_POST['username']); 
    $mypassword = mysqli_real_escape_string($_POST['password']); 
+0

當我通過一個參數..它說警告:mysqli_real_escape_string()期望恰好2個參數,1在C:\ xampp \ htdocs \ index.php在第8行 警告:mysqli_real_escape_string() 2個參數,在第9行的C:\ xampp \ htdocs \ index.php中給出1 –

0

先試試看。

$row = mysqli_fetch_array($result); 

之後,你可以檢查你的錯誤。

mysqli_fetch_array($result) or die(mysqli_error());