Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

php - Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number'

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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 UPDATE firstName = :update_firstName,lastName = :update_lastName,username = :update_username,password = :update_password,email = :update_email,isSuperUser = :update_isSuperUser,dateCreated = :update_dateCreated,dateLastModified = :update_dateLastModified;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...