2014-05-25 2 views
-5

이 코드는 인증, 세션 관리를 사용하여 로그인하는 데 사용됩니다. 치명적인 오류 인 코드의 15 번째 줄에 오류가 있습니다. 객체가 아닌 객체의 멤버 함수인 bindParam()을 호출하십시오. 나는 그 실수가 어디에 있는지 이해하지 못한다. 도와주세요.치명적 오류 : 비 객체의 멤버 함수 bindParam()을 호출하십시오.

<?php 

     // Sanitize incoming username and password 
     $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING); 
     $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING); 
     $pwd= md5($password); 

     // Connect to the MySQL server 
     $db = new mysqli("localhost", "root", "", "login"); 

     // Determine whether an account exists matching this username and password 
     $stmt = $db->prepare("SELECT id FROM accounts WHERE username =$username and password                 =$pwd"); 

     // Bind the input parameters to the prepared statement 
     // the error comes in this line 
     $stmt->bindParam('ss', $username, $pwd); 

     // Execute the query 
     $stmt->execute(); 

     // Store the result so we `enter code here`can determine how many rows have been returned 
     $stmt->store_result(); 

     if ($stmt->num_rows == 1) { 

     // Bind the returned user ID to the $id variable 
     $stmt->bind_result($id); 
     $stmt->fetch(); 

     // Update the account's last_login column 
     $stmt = $db->prepare("UPDATE accounts SET last_login = NOW() WHERE id=$id"); 
     $stmt->bind_param('d', $id); 
     $stmt->execute(); 

     $_SESSION['username'] = $username; 

     // Redirect the user to the home page 
     header('Location: home.php'); 
     } 

    ?> 
+0

가 쿼리를 확인, 그것은 –

+0

을 실패 http://stackoverflow.com/questions/7941089/fatal-error-call-to-a-member-function-bindparam 아차 URL을 입력 한 후에 엔터를 치는 것을 의미하지는 않습니다. 이 질문과 같은 이유. 준비가 실패했습니다. – EPB

답변

-1
$stmt = $db->prepare("SELECT id FROM accounts WHERE username =$username and password=$pwd"); 
$stmt->bindParam('ss', $username, $pwd); 

존재하지 않는 매개 변수를 바인딩하고 있습니다. 하나의 호출로 두 매개 변수를 바인딩하려고합니다. (php.net에서 촬영)

Docs for the relevant function

샘플 :이 mysqli에 대해 사실처럼

<?php 
/* Execute a prepared statement by binding PHP variables */ 
$calories = 150; 
$colour = 'red'; 
$sth = $dbh->prepare('SELECT name, colour, calories 
    FROM fruit 
    WHERE calories < :calories AND colour = :colour'); 
$sth->bindParam(':calories', $calories, PDO::PARAM_INT); 
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12); 
$sth->execute(); 
?> 

[편집]

보인다. Relevant doc

관련 샘플 :

$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)"); 
$stmt->bind_param('sssd', $code, $language, $official, $percent); 
+0

그는'mysqli'를 사용하고 있습니다. –

관련 문제