php - 异常 'PDOException',消息为“SQLSTATE[HY093] : Invalid parameter number: number of bound variables does not match number of tokens”

标签 php mysql sql pdo

阅读之前:我知道表格太奇怪太长,需要标准化;但由于某种原因,这是我的数据库,我应该使用这个表!对于那个很抱歉! 为了保持简短: 我遇到以下异常:

exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in C:\xampp\htdocs\FD\includes\helper.php:472 Stack trace:

0 C:\xampp\htdocs\FD\includes\helper.php(472): PDOStatement->execute(Array) #1

C:\xampp\htdocs\FD\newHrForm.php(113): helperFunctions::UpdateTableHrForms('112', 'dfsfdsfds', '3123213', 'dfdsf', '', '', '', '', '', '', '', '', '', '', '', '3213123213', '1', '3213123', '213123213', '3213213', '213123123', ) #2 {main}

我保存数据的函数如下:

public static function UpdateTableHrForms(
        $id,$dob_city,$dob_province,$dob_country,...) 
            {

                $conn = new mysqlcon();
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $query = "INSERT into `hr_forms` (`id`,`dob_city`,
                    `dob_province`,`dob_country`,...)
                    VALUES (:id,:dob_city,:dob_province,:dob_country,...)";




                try {
                    if (helperFunctions::CheckidExistForms($id) == 0) {
                        $result = $conn->prepare($query);                               
                        $result->execute(array('id'=>$id,'dob_city'=>$dob_city,'dob_province'=>$dob_province,'dob_country'=>$dob_country));



                        $Msg = "<div style=\"text-align:center;\" class=\"alert alert-success\">
                                    <strong>Tips! </strong>
                                        Data is successfully saved to database.
                                    <button class=\"close\" data-dismiss=\"alert\" type=\"button\">&times;</button>
                                </div>";
                    } else {
                        $Msg = "<div style=\"text-align:center;\" class=\"alert alert-error\">
                                    <strong>Error! </strong>
                                        This employee information is already existed in the system.
                                    <button class=\"close\" data-dismiss=\"alert\" type=\"button\">&times;</button>
                                </div>";
                    }
                } catch (Exception $e) {
                    $e->getMessage();
                    $Msg = $e; /* "<div style=\"text-align:center;\" class=\"alert alert-error\">
                      <strong>Error! </strong>
                      This employee information cannot save in the system.
                      <button class=\"close\" data-dismiss=\"alert\" type=\"button\">&times;</button>
                      </div>"; */
                }
                return $Msg;
}

当我替换插入方法的值时(在 UpdateTableHrForms 中使用错误给我的值并在 mysql 中运行它;没有错误;但在 PHP 中它给了我错误;如果我我做错了什么吗?)

最佳答案

您传递给execute的数组格式不正确;您需要在参数名称的开头添加冒号。

应该是:

array(':id'=>$id,':dob_city'=>....

关于php - 异常 'PDOException',消息为“SQLSTATE[HY093] : Invalid parameter number: number of bound variables does not match number of tokens”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18773467/

相关文章:

php - WkHTMLtoPDF 0.12.2.1 PHP exec xvfb-run : Error: xauth command not found

sql - 使用 PIVOT 选择列值作为列

sql - PostgreSQL 为低流量项目存储图像是否合理?

php - Codeigniter 查询生成器,即使表中有数据,我也无法提取行并返回 null

php - 无法将行更新为与当前相同的值吗?

php - mysql ajax更新问题

mysql - 调整 Regex 以与 MySQL 一起工作

php - 从 print_r 输出中删除数组 ( [0] =>

MySql - 时间安排查询

mysql - 如何在 MySQL 子查询中使用 GROUP BY