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(?,?,?) –