2017-08-19 54 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

相关问题