mysqli - 警告和错误

时间:2011-03-21 01:14:18

标签: php mysql database mysqli

我有这个代码,但我有两个错误。我在评论中加入了错误

if(!empty($_POST['email']) && validateEmail($email)) {
    $email = $_POST["email"];
    if ($sql = $db->prepare("select email from users where email=?")) {

        $sql->bind_param('s', $email);

        $sql->execute();

        $sql->bind_result($email);

        while ($sql->fetch()) {
            $salt = "PiuwrO1#O0rl@+luH1!froe*l?8oEb!iu)_1Xaspi*(sw(^&.laBr~u3i!c?es-l651";

            $password = md5($salt . $userExists["email"]);

            $pwrurl = "www.yoursite.com/reset_password.php?q=" . $password;

            $mailbody = "Dear user,<br><br>If this e-mail does not apply to you please ignore it. It appears that you have requested a password reset at our website www.yoursitehere.com<br>
            To reset your password, please click the link below. If you cannot click it, please paste it into your web browser's address bar.<br> <a href='$pwrurl'>$pwrurl</a>  <br> <br>
            Thanks,\nThe Administration";

            $mail->MsgHTML($mailbody);

            $mail->AddAddress("dxxb@hotmail.com","Nome da Pessoa");

            $mail->IsHTML(true);

            if(!$mail->Send()) {
                echo "Deu erro: " . $mail->ErrorInfo;
            } else {
                echo "Enviado com sucesso";
            }
        }
        $sql->close();
        $db->close();
    }

    ($sql = $db->prepare('insert into password_reset (code) values (?)')); // Warning: mysqli::prepare() [mysqli.prepare]: Couldn't fetch mysqli in

    $sql->bind_param('s', $password); // Fatal error: Call to a member function bind_param() on a non-object

    $sql->execute();
    $sql->fetch();
    $sql->close();
    $db->close();
}

所有代码都运行正常,但现在我需要在数据库中插入盐但我不能,我不知道为什么

感谢

编辑上一版本的代码

2 个答案:

答案 0 :(得分:2)

执行查询后,fetch会返回一个结果。可能会有更多 - 可能有很多甚至更多 - 所以你应该在一个循环中调用fetch来获取它们。在完成旧查询之前,您不应该prepare新查询,这通常意味着fetch结果的每一行和close(在您的情况1}}。否则,当您尝试发出另一个请求时,数据库仍处于回答一个请求的过程中。

答案 1 :(得分:0)

第一个错误说明了一切 - 你不能同时在飞行中有超过1个准备好的声明/查询。当您尝试准备另一个语句(select email ...)时,您还没有从第一个查询(insert into ...)中获取数据。