Exception or error:
while I want to insert a post, I can’t get the last id and tag id in the database by using the OOP PHP PDO way. how to use lastInsertId() member function in oop PHP?
here is my post.php
<?php
include_once('DB.php');
class Post
{
private $db;
public function __construct()
{
return $this->db = new DB;
}
public function addPost($tile,$des,$img,$date,$slug)
{
$this->db->query("INSERT INTO posts (title,description,image,created_at,slug) VALUES (:title,:description,:img,:date,:slug)");
$this->db->bind(':title',$tile);
$this->db->bind(':description',$des);
$this->db->bind(':img',$img);
$this->db->bind(':date',$date);
$this->db->bind(':slug',$slug);
// if ($this->db->execute()) {
// echo "Successful";
// } else {
// echo "Failed";
// }
$result = $this->db->execute();
if ($result) {
if ($_POST['tags']) {
$tags = $_POST['tags'];
$lastInsertedId = $this->db->lastInsertId();
foreach ($tags as $tag ) {
$this->db->query("INSERT INTO post_tags (post_id, tag_id) VALUES (:postId,:tagId)");
$this->db->bind(':postID',$lastInsertedId);
$this->db->bind(':tagId',$tag);
$this->db->execute();
}
}
}
return $result;
}
public function getPost()
{
$this->db->query("SELECT * FROM posts");
return $result = $this->db->resultset();
}
function getSinglePost($slug){
$this->db->query("SELECT * FROM posts WHERE slug = :slug");
$this->db->bind(':slug',$slug);
return $result = $this->db->resultset();
}}?>
and my db.php is
<?php
require_once('config/config.php');
class DB
{
private $host = DB_HOST;
private $user = DB_USER;
private $password = DB_PWD;
private $dbname = DB_NAME;
private $connection, $error, $stmt, $dbconnected = false;
public function __construct()
{
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
$option = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => true
);
try {
$this->connection = new PDO($dsn, $this->user, $this->password);
$this->dbconnected = true;
} catch (PDOException $e) {
$this->error = $e->getMessage();
$this->dbconnected = false;
}
}
public function getError()
{
return $this->error;
}
public function isConnected()
{
return $this->dbconnected;
}
public function query($query)
{
$this->stmt = $this->connection->prepare($query);
}
public function lastInsertId()
{
return $this->connection->lastInsertId();
}
public function execute()
{
return $this->stmt->execute();
}
public function resultset()
{
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}
public function rowCount()
{
return $this->stmt->rowCount();
}
public function single()
{
$this->execute();
return $this->stmt->fetch(PDO::FETCH_OBJ);
}
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;
break;
}
}
$this->stmt->bindValue($param, $value, $type);
}}
I can’t configure lastInsertId() function and this occurred problems when $lastInsertedId = $this->db->lastInsertId(); variable can’t binding postID from $_POST request
How to solve: