简体   繁体   中英

How to Use InsertLastId() in PDO

how to use lastInsertId() using pdo, in my code.??

This is my code...

Abstract.php

<?php

require_once 'Database.php';

abstract class Abstract_controller extends Database
{
        /* ==================== POST ==================== */
        protected $post;
        public function showPosts()
        {
                $sql = "SELECT * FROM $this->post";
                $stmt = Database::prepare($sql);
                $stmt->execute();
                return $stmt->fetchAll();
        }

        public function showPostsE()
        {
                $sql = "SELECT * FROM $this->post WHERE id_artikel = :id_artikel";
                $stmt = Database::prepare($sql);
                $stmt->bindParam(':id_artikel', $stmt->lastInsertId('id_artikel'));
                $stmt-execute();
                return $stmt->fetch();
        }

        /* ==================== CATEGORIES ==================== */
        protected $categories;
        public function showCategories()
        {
                $sql = "SELECT * FROM $this->categories";
                $stmt = Database::prepare($sql);
                $stmt->execute();
                return $stmt->fetchAll();
        }
}

?>

controller.php

<?php

require_once 'Abstract_controller.php';

class Class_controller extends Abstract_controller
{
        /* ==================== POST ==================== */
        protected $post = "artikel";
        private $id_artikel;
        private $judul_artikel;
        private $isi_artikel;
        private $categories_artikel;

        public function setJudulArtikel($judul_artikel)
        {
                $this->judul_artikel = $judul_artikel;
        }

        public function setIsiArtikel($isi_artikel)
        {
                $this->isi_artikel = $isi_artikel;
        }

        public function setCategArtikel($categories_artikel)
        {
                $this->categories_artikel = $categories_artikel;
        }

        /* ==================== SAVE POSTS ==================== */
        public function savePosts()
        {
                $sql = "INSERT INTO artikel (id_artikel, judul_artikel, isi_artikel, status, categories)
                                VALUES (NULL, :judul_artikel, :isi_artikel, 'save', :categories_artikel)";
                $stmt = Database::prepare($sql);
                $stmt->bindParam(':judul_artikel', $this->judul_artikel);
                $stmt->bindParam(':isi_artikel', $this->isi_artikel);
                $stmt->bindParam(':categories_artikel', $this->categories_artikel);
                $stmt->lastInsertId(PDO::ERRMODE_EXCEPTION);
                return $stmt->execute();
        }

        /* ==================== PUBLISH POSTS ==================== */
        public function publishPosts()
        {
                $sql = "INSERT INTO artikel (id_artikel, judul_artikel, isi_artikel, status)
                                VALUES (NULL, :judul_artikel, :isi_artikel, 'publish')";
                $stmt = Database::prepare($sql);
                $stmt->bindParam(':judul_artikel', $this->judul_artikel);
                $stmt->bindParam(':isi_artikel', $this->isi_artikel);
                return $stmt->execute();
        }

        /* ==================== CATEGORIES ==================== */
        protected $categories = "categories";

}



?>

For getting last inserted id, you should use lastInsertId() with connection , not with statement .

public function savePosts()
        {
                $sql = "INSERT INTO artikel (id_artikel, judul_artikel, isi_artikel, status, categories)
                                VALUES (NULL, :judul_artikel, :isi_artikel, 'save', :categories_artikel)";
                $stmt = Database::prepare($sql);
                $stmt->bindParam(':judul_artikel', $this->judul_artikel);
                $stmt->bindParam(':isi_artikel', $this->isi_artikel);
                $stmt->bindParam(':categories_artikel', $this->categories_artikel);
                $stmt->execute();

                return Database::lastInsertId();  // here you can return last inserted id
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM