【问题标题】:A new error Invalid parameter number: number of bound variables does not match number of tokens一个新错误 Invalid parameter number: number of bound variables does not match number of tokens
【发布时间】:2018-06-01 12:59:00
【问题描述】:

我正在运行更新脚本并收到此错误:

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;
    }
}

【问题讨论】:

标签: php html sass


【解决方案1】:

你不见了:

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

你的函数中可能还有一个$id 参数:

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

那么完整的代码应该是(假设$idupdate() 的参数):

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;
    }
}

【讨论】:

  • 感谢@Brent,我根据您的建议处理了代码,现在我遇到了另一个错误 - SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
  • 你已经有一个id_number...你还需要一个单独的id吗?
  • 您的 sql 中有错字。它说:=:firsname 但它应该是=:firstname。缺少“t”。 =:identiy 也一样,应该是 =:identity
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多