简体   繁体   中英

PHP prepared statement error

So I'm trying to update some stuff in my database when the user logs on, it seems that everything is fine except the last part, the output I am getting is:

"critical error unknown" and "Commands out of sync; you can't run this command now"

Here is my code:

<?php
$user = $_POST['username'];
$pass = $_POST['password'];
$connect = mysqli_connect("localhost","myuser","mypass","somedb");
$query = "SELECT `password` FROM `userauth` WHERE `username`=?";
if($stmt = $connect->prepare($query)){
    $stmt->bind_param('s',$user);
    $stmt->execute();
    $stmt->bind_result($tmp_pass);
    $stmt->fetch();
    if($tmp_pass===$pass){
        $letters = array('a','b','*','x','e','d','z','p','@','#');
        $letter_key="";
        for($i=0;$i<sizeof($letters);$i++){
            $letter_key=$letter_key.$letters[rand(1,sizeof($letters-1))];
        }
        $key = rand(1341163,9999999);
        $key2 = rand(3541,9999);
        $complete_key = $key.$letter_key.$key2;
        setcookie("key",$complete_key);
        setcookie("user",$user);
        $query_auth = "UPDATE `userauth` SET `auth_key`=? WHERE `username`=?";
        if($stmt_2 = $connect->prepare($query_auth)){
            $stmt_2->bind_param('ss',$complete_key,$user);
            $stmt_2->execute();
        }else{
            echo "Critical error, unknown ".mysqli_error($connect);
            exit;
        }
        echo "success";
    }else{
        echo "Error Invalid Username or Password";
    }
}else{
    "Database link error";
}
?>

Can someone please explain what might be the problem. yes, all my fields are string.

在$ stmt-> fetch()之后尝试$ stmt-> close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM