1
同时插入新的数据到数据库警告:PDOStatement对象::执行():SQLSTATE [HY093]
警告我都遇到过这样的错误:PDOStatement对象::执行():SQLSTATE [HY093]:无效参数 号:
:绑定变量的数目不 /opt/lampp/htdocs/projectclasses/DB.php在线47上public function query($sql, $params = array()) { $this->_error = false; if($this->_query = $this->_pdo->prepare($sql)) { if(count($params)) { foreach($params as $param) { $x = 1; $this->_query->bindParam($x, $param); $x++; } } if($this->_query->execute()) { $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); $this->_count = $this->_query->rowCount(); } else { $this->_error = true; } } return $this; } public function insert($table, $fields = array()) { if(count($fields)) { $keys = array_keys($fields); $values = ''; $x = 1; foreach($fields as $field) { $values .= "?"; if($x < count($fields)) { $values .= ", "; } $x++; } $sql = "INSERT INTO users (`". implode('` ,`', $keys) ."`) VALUES (".$values.")"; if(!$this->query($sql, $fields)->error()) { return true; } } return false; }
的方法的代码匹配的令牌数3210
插入的代码:
$user = DB::getInstance()->insert('users', array(
'username' => 'Marco',
'password' => '123456',
'salt' => 'salt'
));
echo你的$ sql变量,你看到了什么? – Clay
INSERT INTO users('username','password','salt')VALUES(?,?,?) –