Wednesday, January 15, 2020

Guru Design

CRUD Insert Data PDO OOP

Kali ini guru design akan berbagi tutorial CRUD Insert data berbasis object oriented,. Ok lanjut ke Kang Yusuf tutorialnya:

Buat database dengan nama db_latihanobjek dan tabel dengan nama artikel

CREATE TABLE `artikel` (
    `id_artikel` int(5) NOT NULL AUTO_INCREMENT,
    `kategori` varchar(20) NOT NULL,
    `judul` varchar(20) NOT NULL,
    `isi` text NOT NULL;

Buat dua folder dengan nama view dan modal

Pada folder view buatlah file artikel.php

artikel.php
<?php
    error_reporting('~$_NOTICE');
    include "../model/class-insert.php";  
    $auth_artikel = new Insertdata();

    //Untuk melakukan eksekusi insert data
    if (isset($_POST['kirim'])) {
        $kategori   = ($_POST['kategori']);
        $judul  = ($_POST['judul']);
        $isi  = ($_POST['isi']);
        if ($kategori == "") {
            $error[]    = "kategori kosong!";
        }
         elseif ($judul == "") {
            $error[]    = "judul kosong!";
        }
         elseif ($isi == "") {
            $error[]    = "isi kosong!";
        }
            try {
                if ($auth_artikel->insertartikel($kategori, $judul, $isi)) {
                    $auth_artikel->redirect('artikel.php');
                }
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
    }
 ?>
<!DOCTYPE html>
<html>
<head>
  <title>CRUD Insert PDO OOP</title>
</head>
<body>
  <form method="post">
    <select name="kategori" required="">
      <option value="">Pilih Kategori</option>
      <option value="pendidikan">Pendidikan</option>
      <option value="pertanian">Pertanian</option>
      <option value="perdagangan">Perdagangan</option>
    </select>
    <br>
    <input type="text" name="judul" placeholder="judul" required="">
    <br>
    <textarea type="text" name="isi" placeholder="isi"></textarea>
    <button type="submit" name="kirim" title="Posting" >Posting</button>
  </form>
</body>
</html> 

Pada folder modal buatlah file koneksi.php dan class-insert.php

koneksi.php
<?php
    /**
     * Database
     */
    class Database
    {
        private $hostname   = "localhost";
        private $db_name    = "db_latihanobjek";
        private $username   = "root";
        private $password   = "";
        public $conn;
        public function dbConnection()
        {
            $this->conn = null;
            try {
                $this->conn =new PDO("mysql:host=" . $this->hostname . ";dbname=" . $this->db_name, $this->username, $this->password);
                $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $e) {
                echo "Connection error: " . $e->getMessage();
            }
            return $this->conn;
        }
    }
?>

class-insert.php
<?php

    include "koneksi.php";
    /**
     * Insertdata Class
     */
    class Insertdata
    {
        private $conn;
        public function __construct()
        {
            $database   = new Database();
            $db         = $database->dbConnection();
            $this->conn = $db;
        }

        //star artikel-----------------------------------------------
        public function insertartikel($kategori, $judul, $isi)
        {
            try {
                $stmt = $this->conn->prepare('INSERT INTO artikel (kategori, judul, isi VALUES (:1, :2, :3)');
                $stmt->bindParam(':1',$kategori);
                $stmt->bindParam(':2',$judul);
                $stmt->bindParam(':3',$isi);
                $stmt->execute();
                return $stmt;
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }
        //end artikel-----------------------------------------------
        public function redirect($url, $statusCode = 303)
        {
            header('Location: ' . $url, true, $statusCode);
            die();
        }
    }
?>

Sekian Semoga bermanfaat.

 

No comments:

Post a Comment