SQLSTATE [HY093]:错误但我不明白为什么

时间:2014-04-14 18:31:49

标签: php mysql windows

所以这是代码:     尝试{

        $db->beginTransaction();
        $ipaddress = getenv('REMOTE_ADDR');
        $stmt2 = $db->prepare("INSERT INTO members (username, email, password, signup_date, ipaddress) VALUES (:username, :email1, :bcrypt, now(), :ipaddress)");
        $stmt2->bindParam(':username', $username, PDO::PARAM_STR);
        $stmt2->bindParam(':email1', $email1, PDO::PARAM_STR);
        $stmt2->bindParam(':bcrypt',$bcrypt, PDO::PARAM_STR);
        $stmt->bindParam(':ipaddress', $ipaddress, PDO::PARAM_INT);
        $stmt->execute();
        //get the last id inserted to the db which is now this users id for activation and member folder creation///
        $lastId = $db->lastInsertId();
        $stmt3 = $db->prepare("INSERT INTO activate (user, token) VALUES (:lastId , :token)");
        $stmt3->bindValue(':lastId', $lastId, PDO::PARAM_STR);
        $stmt3->bindValue(':token', $token, PDO::PARAM_STR);
        $stmt3->execute();
        //send email activation to new user///
        $from = "From: Auto Responder @ geekifyme <admin@geekifyme.org>";
        $subject = "IMPORTANT: Activate your geekifyme account";
        $link = "http://www.geekifyme.org/scripts/activate.php?user='.$lastId.'$token='.$token.";
        //strt email body////
        $message = "
        Thanks for registering an account at GeekifyMe!  There is just one last step in setting up your account.  Please click the link below to confirm your identity and get started.  If the link below is not active please copy and paste it into your browser bar.

        $link
        ";
        //set headers////
        $headers = 'MIME-Version: 1.0' . "rn";
        $headers .= "Content_type: textrn";
        $headers .=  "From: $fromrn";
        //send the email now/////
        mail($email1, $subject, $message, $headers);
        $db->commit();
        echo 'Thanks for joining! Check your email in a few moments to activate your account so that you may log in.';
        $db = null;
        exit();
    }
    catch(PDOException $e){
        $db->rollBack();
        echo $e->getMessage();;
        $db = null;
        exit();
    }

是什么导致这种情况?

SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配

我没有看到任何基本忘记的&#39;:&#39;或任何其他真正基本的混乱。你能看到什么吗?

3 个答案:

答案 0 :(得分:2)

    $stmt2 = $db->prepare("INSERT INTO members (username, email, password, signup_date, ipaddress) VALUES (:username, :email1, :bcrypt, now(), :ipaddress)");
         ^---- note the 2
    $stmt->bindParam(':ipaddress', $ipaddress, PDO::PARAM_INT);
         ^--- note the LACK of a 2
    $stmt->execute();
         ^--- ditto

您将ipaddress参数绑定到完全不同的语句。然后你尝试执行完全不同的声明。

答案 1 :(得分:1)

您正在创建一个声明对象 stmt2

在接下来的三行中引用它。

但接下来的两行是 stmt ,而不是 stmt2

我们在您的代码示例中没有看到SQL文本或 stmt 的准备工作,我们看不到 stmt2 <的执行/强>

答案 2 :(得分:0)

这里在第一个插入查询中传递5个参数并仅绑定4个参数。检查这个。我认为错误可能就在这里。

    $stmt2 = $db->prepare("INSERT INTO members (username, email, password, signup_date, ipaddress) VALUES (:username, :email1, :bcrypt, now(), :ipaddress)");
    $stmt2->bindParam(':username', $username, PDO::PARAM_STR);
    $stmt2->bindParam(':email1', $email1, PDO::PARAM_STR);
    $stmt2->bindParam(':bcrypt',$bcrypt, PDO::PARAM_STR);
    $stmt2->bindParam(':ipaddress', $ipaddress, PDO::PARAM_INT);
    $stmt2->execute();

将此行替换为您的代码并进行检查。