首页 > 解决方案 > 新错误 Invalid parameter number: number of bound variables does not match number of tokens

问题描述

我正在运行更新脚本,但出现此错误:

SQLSTATE[HY093]:无效的参数号:绑定变量的数量与标记的数量不匹配。

下面是我的代码: class.update.php

public function update($firstname, $lastname, $postaladdress, $mobilephone, $officephone, $personalemail, $officialemail, $nationality, $city, $identity, $id_number, $gender, $dob)
{
    try
    {
        $stmt=$this->db->prepare
        ("UPDATE clients SET
                 firstname      =:firsname, 
                 lastname       =:lastname, 
                 postaladdress  =:postaladdress, 
                 mobilephone    =:mobilephone,
                 officephone    =:officephone,
                 personalemail  =:personalemail,
                 officialemail  =:officialemail,
                 nationality    =:nationality,
                 city           =:city,
                 identity       =:identiy,
                 id_number      =:id_number,
                 gender         =:gender,
                 dob            =:dob
          WHERE client_id=:id");

        $stmt->bindparam(":firstname",$firstname);
        $stmt->bindparam(":lastname",$lastname);
        $stmt->bindparam(":postaladdress",$postaladdress);
        $stmt->bindparam(":mobilephone",$mobilephone);
        $stmt->bindparam(":officephone",$officephone);
        $stmt->bindparam(":personalemail",$personalemail);
        $stmt->bindparam(":officialemail",$officialemail);
        $stmt->bindparam(":nationality",$nationality);
        $stmt->bindparam(":city",$city);
        $stmt->bindparam(":identity",$identity);
        $stmt->bindparam(":id_number",$id_number);
        $stmt->bindparam(":gender",$gender);
        $stmt->execute();

        return true;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();  
        return false;
    }
}

标签: phphtmlsass

解决方案


你错过了:

$stmt->bindparam(":dob",$dob);
$stmt->bindparam(":id",$id);

并且可能是$id您的函数中的一个参数:

public function update($firstname, $lastname, etc etc etc, $id)

完整的代码应该是(假设$id是一个参数update()):

public function update($firstname, $lastname, $postaladdress, $mobilephone, $officephone, $personalemail, $officialemail, $nationality, $city, $identity, $id_number, $gender, $dob, $id)
{
    try
    {
        $stmt=$this->db->prepare
        ("UPDATE clients SET
                 firstname      =:firstname, 
                 lastname       =:lastname, 
                 postaladdress  =:postaladdress, 
                 mobilephone    =:mobilephone,
                 officephone    =:officephone,
                 personalemail  =:personalemail,
                 officialemail  =:officialemail,
                 nationality    =:nationality,
                 city           =:city,
                 identity       =:identity,
                 id_number      =:id_number,
                 gender         =:gender,
                 dob            =:dob
          WHERE client_id=:id");

        $stmt->bindparam(":firstname",$firstname);
        $stmt->bindparam(":lastname",$lastname);
        $stmt->bindparam(":postaladdress",$postaladdress);
        $stmt->bindparam(":mobilephone",$mobilephone);
        $stmt->bindparam(":officephone",$officephone);
        $stmt->bindparam(":personalemail",$personalemail);
        $stmt->bindparam(":officialemail",$officialemail);
        $stmt->bindparam(":nationality",$nationality);
        $stmt->bindparam(":city",$city);
        $stmt->bindparam(":identity",$identity);
        $stmt->bindparam(":id_number",$id_number);
        $stmt->bindparam(":gender",$gender);
        $stmt->bindparam(":dob",$dob);
        $stmt->bindparam(":id",$id);
        $stmt->execute();

        return true;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();  
        return false;
    }
}

推荐阅读