Вот мой код, я пытаюсь вставить данные в базу данных, используя PDO, я не знаю, где произошел сбой моего кода, который не вставляет и возвращает false в lastInsertId (). Посмотрите на мой код:
Model.php
<?php
abstract class Model{
protected $dbh;
protected $stmt;
public function __construct(){
$this->dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
}
public function error(){
print_r($this->dbh->errorInfo());
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
//Binds the prep statement
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
}
функция регистрации: (это моя функция регистрации, где находится запрос вставки).
public function register(){
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$pass = md5($post['password']);
if(isset($post['submit']))
{
$this->query('INSERT INTO tbl_users (firstname, lastname, email, password, gender, dob) VALUES(:firstname, :lastname, :email, :password, :gender, :dob)');
$this->bind('firstname', $post['fname']);
$this->bind('lastname', $post['lname']);
$this->bind('email', $post['email']);
$this->bind('password', $pass);
$this->bind('gender', $post['gender']);
$this->bind('dob', $post['date']);
$this->execute();
if($this->lastInsertId()){
header('Location:'.ROOT_PATH.'admin/user');
}else{
echo "wrong";
$this->error();
}
}
}
Может кто-нибудь сказать мне, где я сделал неправильно, и почему это происходит, любая помощь будет оценена. Заранее спасибо.