<tfoot id='ugQvu'></tfoot>
<i id='ugQvu'><tr id='ugQvu'><dt id='ugQvu'><q id='ugQvu'><span id='ugQvu'><b id='ugQvu'><form id='ugQvu'><ins id='ugQvu'></ins><ul id='ugQvu'></ul><sub id='ugQvu'></sub></form><legend id='ugQvu'></legend><bdo id='ugQvu'><pre id='ugQvu'><center id='ugQvu'></center></pre></bdo></b><th id='ugQvu'></th></span></q></dt></tr></i><div id='ugQvu'><tfoot id='ugQvu'></tfoot><dl id='ugQvu'><fieldset id='ugQvu'></fieldset></dl></div>
    • <bdo id='ugQvu'></bdo><ul id='ugQvu'></ul>

      <small id='ugQvu'></small><noframes id='ugQvu'>

      <legend id='ugQvu'><style id='ugQvu'><dir id='ugQvu'><q id='ugQvu'></q></dir></style></legend>

    1. 未捕获的异常 'PDOException' 带有消息 'S

      时间:2023-10-05
        <tfoot id='4V7Tm'></tfoot>
          <bdo id='4V7Tm'></bdo><ul id='4V7Tm'></ul>
            <tbody id='4V7Tm'></tbody>

          <small id='4V7Tm'></small><noframes id='4V7Tm'>

            <legend id='4V7Tm'><style id='4V7Tm'><dir id='4V7Tm'><q id='4V7Tm'></q></dir></style></legend>

              • <i id='4V7Tm'><tr id='4V7Tm'><dt id='4V7Tm'><q id='4V7Tm'><span id='4V7Tm'><b id='4V7Tm'><form id='4V7Tm'><ins id='4V7Tm'></ins><ul id='4V7Tm'></ul><sub id='4V7Tm'></sub></form><legend id='4V7Tm'></legend><bdo id='4V7Tm'><pre id='4V7Tm'><center id='4V7Tm'></center></pre></bdo></b><th id='4V7Tm'></th></span></q></dt></tr></i><div id='4V7Tm'><tfoot id='4V7Tm'></tfoot><dl id='4V7Tm'><fieldset id='4V7Tm'></fieldset></dl></div>
                本文介绍了未捕获的异常 'PDOException' 带有消息 'SQLSTATE[HY093]: Invalid parameter number'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                每当我尝试使用 PDO 插入我的数据库时,我都会收到错误消息.

                I'm receiving an error whenever I attempt to insert into my database using PDO.

                public function save($primaryKey = "") {
                        $validate = $this->rules();
                        if ($validate === true) {
                            $properties = '';
                            $values = '';
                            $bindings = array();
                            $update = '';
                            foreach ($this as $property => $value){
                                if ($property === "conn") {
                                    continue;
                                }
                                $properties .= $property . ',';
                                $values .= ':' . $property . ',';
                                $update .= $property . ' = :' . $property . ',';
                                $bindings[':'.$property] = $value;
                            }
                            $sql_string = 'INSERT INTO ' . get_class($this) . ' (' . rtrim($properties, ',') . ') ';
                            $sql_string .= 'VALUES (' . rtrim($values, ',') . ') ON DUPLICATE KEY UPDATE ' . rtrim($update, ',') . ';';
                            $result = $this->executeQuery(NULL, $sql_string, $bindings);
                            $this->buildObject($result);
                            if (!empty($primaryKey)) {
                                $this->$primaryKey = $this->conn->lastInsertId();
                            }
                            return $result;
                        } else {
                            return $validate;
                        }
                    }
                
                public function executeQuery($object, $sql_string, $bindings = null) {
                        $stmt = $this->conn->prepare($sql_string);
                        if (!empty($bindings)) {
                            if (!$stmt->execute($bindings)) {return false;}
                        } else {
                            if (!$stmt->execute()) {return false;}
                        }
                        $result = (!empty($object) ? $stmt->fetchAll(PDO::FETCH_CLASS, $object) : $stmt->fetchAll());
                        return (($stmt->rowCount() > 0) ? $result : false);
                    }
                

                save 函数生成查询字符串和绑定,两者看起来都是正确的.

                The save function generates both the query string and the bindings which both seem correct.

                query = INSERT INTO am_administrator (firstName,lastName,username,password,email,isSuperUser,dateCreated,dateLastModified) VALUES (:firstName,:lastName,:username,:password,:email,:isSuperUser,:dateCreated,:dateLastModified) ON DUPLICATE KEY UPDATE firstName = :firstName,lastName = :lastName,username = :username,password = :password,email = :email,isSuperUser = :isSuperUser,dateCreated = :dateCreated,dateLastModified = :dateLastModified;
                
                bindings = array(8) { 
                [":firstName"]=> string(5) "First" 
                [":lastName"]=> string(4) "Last" 
                [":username"]=> string(7) "cova-fl" 
                [":password"]=> string(8) "password" 
                [":email"]=> string(16) "test@testing.com" 
                [":isSuperUser"]=> int(1) "1" 
                [":dateCreated"]=> string(19) "2016-05-11 02:40:15" 
                [":dateLastModified"]=> string(19) "2016-05-11 02:40:15" 
                }
                

                每当我将查询放入工作台时,我都没有问题,但是当我尝试在代码中运行它时,我收到致命错误:未捕获的异常 'PDOException',消息为 'SQLSTATE[HY093]:无效的参数编号',这让我感到困惑,因为绑定参数的数量与绑定键和数字匹配.任何人都可以在这个问题上启发我吗?

                Whenever I put the query into workbench I have no problems, but when trying to run it in code I get Fatal Error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number' which confuses me since the number of bindings params matches the bindings keys and nummbers. Can anyone enlighten me on this issue?

                推荐答案

                我认为这可能是因为您在语句中对每个绑定进行了两次 decared,例如:firstname 出现在 VALUES 子句以及 ON DUPLICATE KEY UPDATE 子句中.

                I think this might be because you have decared each binding twice in the statement e.g. :firstname appears in the VALUES clause as well as the ON DUPLICATE KEY UPDATE clause.

                您只向 $stmt->execute 传递了 8 个绑定,但 PDO 正在寻找 16 个.

                You only pass 8 bindings to the $stmt->execute but PDO is looking for 16.

                您可以尝试在 ON DUPLICATE KEY UPDATE 子句中对它们的命名略有不同,为您提供一个查询,例如

                You could try naming them slightly different in the ON DUPLICATE KEY UPDATE clause giving you a query such as e.g.

                <代码>INSERT INTO am_administrator (firstName,lastName,username,password,email,isSuperUser,dateCreated,dateLastModified) VALUES (:firstName,:lastName,:username,:password,:email,:isSuperUser,:dateCreated,:dateLastModified) ON DUPLICATE KEY UPDATEfirstName = :update_firstName,lastName = :update_lastName,username = :update_username,password = :update_password,email = :update_email,isSuperUser = :update_isSuperUser,dateCreated = :update_dateCreated,dateLastModified = :update_dateLastModified;

                这篇关于未捕获的异常 'PDOException' 带有消息 'SQLSTATE[HY093]: Invalid parameter number'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:PHP 数据库 PDO 连接 下一篇:Php PDO rowCount() 返回错误结果

                相关文章

                最新文章

                  <i id='Rz0qQ'><tr id='Rz0qQ'><dt id='Rz0qQ'><q id='Rz0qQ'><span id='Rz0qQ'><b id='Rz0qQ'><form id='Rz0qQ'><ins id='Rz0qQ'></ins><ul id='Rz0qQ'></ul><sub id='Rz0qQ'></sub></form><legend id='Rz0qQ'></legend><bdo id='Rz0qQ'><pre id='Rz0qQ'><center id='Rz0qQ'></center></pre></bdo></b><th id='Rz0qQ'></th></span></q></dt></tr></i><div id='Rz0qQ'><tfoot id='Rz0qQ'></tfoot><dl id='Rz0qQ'><fieldset id='Rz0qQ'></fieldset></dl></div>
                  <tfoot id='Rz0qQ'></tfoot>

                    <bdo id='Rz0qQ'></bdo><ul id='Rz0qQ'></ul>

                  <small id='Rz0qQ'></small><noframes id='Rz0qQ'>

                1. <legend id='Rz0qQ'><style id='Rz0qQ'><dir id='Rz0qQ'><q id='Rz0qQ'></q></dir></style></legend>