2017-08-19 53 views
0

出於某種原因,我的準備聲明似乎沒有返回任何行或登錄,但我確定輸入的密碼和用戶名是正確的;在沒有準備語句的情況下運行代碼時,系統會登錄。是否有人會知道我的準備語句不工作的原因,或者是否有其他方法可以使用?爲什麼我的準備聲明不起作用?

<?php session_start(); ?> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Logged in</title> 
</head> 
<body> 
    <?php 

     $user=$_GET["username"]; 
     $pass=$_GET["password"]; 


     $servername="localhost"; 
     $username="root"; 
     $password=""; 
     $dbName="db_artzytest"; 

     $conn=new mysqli($servername, $username, $password, $dbName); 

     if($conn->connect_error){ 
      echo $conn->connect_error; 
      die("connection to server not found"); 
     }else{ 
      echo "connection established"; 
     } 
     ///finds account with matching login info 
     $sql="SELECT * FROM db_users WHERE username='{$user}' AND password='{$pass}' "; 


     $sql="SELECT * FROM db_users WHERE username = ? AND password = ?"; 


     try{ 
      $stmt = $conn->prepare($sql); 
      $stmt->bind_param("ss", $user, $pass); 
      $stmt->execute(); 
     }catch(Exception $e){ 
      die("prepare failed: " . $e); 
     } 
     echo 'here'; 
     //$result = $stmt->store_result(); 

     printf("Number of rows: %d. \n", $stmt->num_rows); 

     //$result=$conn->query($sql); 
     echo 'here'; 
     if(mysqli_num_rows($result) > 0){ 

      while($row=mysqli_fetch_assoc($result)){ 
       //only logs in if the account is activated 
       if($row["isActivated"]==1){ 

        $_SESSION["currentUser"]=$user; 
        $_SESSION["currentId"]=$row["id"]; 
        $_SESSION["currentPass"]=$pass; 

        //header('Location: ../ProfilePage/profilePage.php'); 
        header('Location: ../Content/displayGroup.php?group=general'); 
       }else{ 
        $_SESSION["m_Login"]="Unverified Account. Check your email to verify."; 
        header('Location: Login.php'); 
       } 
      } 
      /* 
      $sql=" SELECT * FROM table_images WHERE userid = {$_SESSION["currentId"]} "; 

      $result=$conn->query($sql); 

      if(mysqli_num_rows($result) > 0){ 
       while($row=mysqli_fetch_assoc($result)){ 
        echo "<img style='height: 10vh; width: 10vw;' src='../../images/{$row["id"]}.jpg' />"; 
        echo "{$row["imageName"]}</br>"; 
       } 
      } 
      */ 

     }else{ 
      $_SESSION["m_Login"]="password or username incorrect {$user} {$pass}" . var_dump(mysqli_stmt_get_result($stmt)); 
      header('Location: Login.php'); 
      echo "no user found"; 
     } 

    ?> 
</body> 
</html> 
+1

Java在哪裏? – Script47

+0

基本調試:確保'$ _GET'不是'$ _POST',並且數據正在傳遞。爲什麼你要定義'$ sql'並且立刻用問號參數重新定義它?總是在'header'中使用'exit'來防止錯誤。 – Script47

回答

0

您正在混合OO和procdeural,所以我將假設這是什麼導致您的代碼不按預期執行。

的文檔狀態:

程序僅樣式:()由mysqli_query返回的結果集標識符,mysqli_store_result()或mysqli_use_result()。

變化,

if(mysqli_num_rows($result) > 0){ 

要,

if($stmt->num_rows($result) > 0) { 

這應該解決這個問題。

+0

哦,對不起,我的代碼有點令人困惑,$ result變量在沒有prepare語句的情況下從登錄代碼中遺留下來。現在應該設置什麼結果? – user2350459

相關問題