2017-03-11 1 views
1

아직 PHP에 비교적 익숙하지 않아서 쉽게 실수를 저지르고있을 수도 있습니다.PHP에서 오류가 발생하는 2 개의 간단한 SQL 쿼리

아래의 코드는 경고가 발생합니다 : 첫 번째는 잘 작동하고 두 번째는 이러한 오류를 throw로

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /home/unn_w14017307/public_html/Assignment3/JakeUploads/11March/logonProcess.php on line 87 

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in /home/unn_w14017307/public_html/Assignment3/JakeUploads/11March/logonProcess.php on line 88 

Warning: mysqli_stmt_store_result() expects parameter 1 to be mysqli_stmt, boolean given in /home/unn_w14017307/public_html/Assignment3/JakeUploads/11March/logonProcess.php on line 89 

Warning: mysqli_stmt_bind_result() expects parameter 1 to be mysqli_stmt, boolean given in /home/unn_w14017307/public_html/Assignment3/JakeUploads/11March/logonProcess.php on line 90 

내가,이 내가 실행할 첫 번째 쿼리와 함께 할 수있는 뭔가가 가정합니다.

첫 번째 쿼리에서 $ conn이 제대로 작동하므로 연결 문제가 아닙니다.

<?php 
     $username = filter_has_var(INPUT_POST, 'username') ? $_POST['username']: null; 
     $password = filter_has_var(INPUT_POST, 'password') ? $_POST['password']: null; 

     include 'database_conn.php'; // make db connection 

     /* Query the users database table to get the password hash for the username entered by the user in the logon form */ 

     $sql = "SELECT password FROM Users WHERE username = ?"; 
     $typeSql = "SELECT type FROM Users WHERE username = $username"; 

     $stmt = mysqli_prepare($conn, $sql); // prepare the sql statement 

     /* Bind the $username entered by the user to the prepared statement. Note the “s” part indicates the data type used – in this case a string */ 

     mysqli_stmt_bind_param($stmt, "s", $username);  

     mysqli_stmt_execute($stmt); // execute the query 
     mysqli_stmt_store_result($stmt); //store result so second query can be used 
     /* Get the password hash from the query results for the given username and store it in the variable indicated */ 

     mysqli_stmt_bind_result($stmt, $returnedPass); 

     /* Check if a record was returned by the query. If yes, then there was a username matching what was entered in the logon form and we can now test to see if the password entered in the logon form is the same as the stored (correct) one in the database. */ 

     if (mysqli_stmt_fetch($stmt)) { 
      if($password === $returnedPass) 
      { 
       mysqli_stmt_close($stmt); 
       $stmt = mysqli_prepare($conn, $typeSql); 
       mysqli_stmt_bind_param($stmt, "s", $username); 
       mysqli_stmt_execute($stmt); 
       mysqli_stmt_store_result($stmt); 
       mysqli_stmt_bind_result($stmt, $accountType); 


       $_SESSION['uName'] = $username; 
       $_SESSION['logged-in'] = true; 
       $_SESSION['type'] = $type; 

       echo "<p>Password valid</p>"; 
       echo "<p>Username: $username</p>"; 
       echo "<p>Account type: $type</p>"; 


      } 
      else 
      { 
       echo "<p>Password invalid</p>"; 
      } 
     } 
     else { 
      echo "<p>Sorry we don't seem to have that username.</p>"; 
     } 

     mysqli_stmt_close($stmt); 
     mysqli_close($conn); 
    ?> 
+0

(!) 참고 : 내가 좋아하는 일을 할 것, 안전한 로그인을위한 그러나

SELECT password, type FROM Users WHERE username = ? 

: 왜 당신은 두 개의 별도의 쿼리를 사용하는

또 다른 점은이에 대한 쿼리를 결합 할 수 있다는 것입니다 ? 이 작업을 하나의 작업으로 수행 할 수 있습니다. –

+0

쿼리에 대해 mysqli_error ($ conn)로 실제 오류가 있는지 확인하십시오. –

+0

하나의 쿼리에서이 작업을 어떻게 수행합니까? mysqli_stmt_bind_result를 사용하여 쿼리에서 암호와 결과를 별도의 변수로 분리하는 방법을 잘 모르겠습니다. –

답변

1

범인은 mysqli 만 ?에 매개 변수를 결합 할 수 있다는 것입니다 :

여기에 코드입니다.

$sql = "SELECT password FROM Users WHERE username = ?"; 
$typeSql = "SELECT type FROM Users WHERE username = ?"; 

그러나, 당신이 당신의 문 한 번만 준비해야합니다 @Fred로 보조 노트에

if(! $typestmt = mysqli_prepare($conn, $typeSql)){ 
    die('error in sql syntax'); 
} 

if (mysqli_stmt_fetch($stmt)) { 
    if($password === $returnedPass) 
    { 
    mysqli_stmt_bind_param($typestmt, "s", $username); 
    mysqli_stmt_execute($typestmt); 
    mysqli_stmt_store_result($typestmt); 
    mysqli_stmt_bind_result($typestmt, $accountType); 
    } 
} 

일반 텍스트 암호를 저장하는 것은 안전하지 않습니다 말하고있다.

password_hash()을 사용하여 해시를 생성하고 암호로 저장하고 해당 값을 검색 한 다음 사용자가 제공 한 값으로 데이터베이스에 저장된 해시와 함께 password_verify()을 사용하여 로그인을 확인하십시오.

SELECT id FROM users WHERE username = ? AND password = ?" 
+0

진술을 한 번만 작성해야한다는 것은 정확히 무엇을 의미합니까? 조금 더 확장하여 제가 잘못하고있는 것을 보여줄 수 있습니까? 조금 더 이해하려고 노력하면서 동일한 실수를 저 지르지 마십시오. –

+0

문은 실제로 빈칸을 채우는 쿼리입니다. 새 업데이트 된 값을 다시 바인딩하고 다시 실행할 수 있습니다. – Xorifelse

관련 문제