Rizkhal

OOP

Jun 22nd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.19 KB | None | 0 0
  1. #edit_action.php
  2. <?php
  3.     require 'library.php';
  4.     if (isset($_POST['submit'])) {
  5.  
  6.         $id = $_GET['kode'];
  7.         $buku = $_POST['buku'];
  8.         $penerbit = $_POST['penerbit'];
  9.  
  10.         $up = $buku->updateBuku($id, $judul, $penerbit);
  11.         if ($up == 'Seccess') {
  12.             header('Location:index.php');
  13.         }else{
  14.             echo "failed!";
  15.         }
  16.     }
  17. ?>
  18.  
  19. #library.php
  20. <?php
  21.  
  22.     /**
  23.      * library.php
  24.      * connection, show, add, edit, delete!
  25.      */
  26.  
  27.     class library{
  28.  
  29.         public function __construct()
  30.         {
  31.             $this->db = new PDO('mysql:host=localhost;dbname=crudbuku', 'root', '');
  32.         }
  33.  
  34.         public function addBuku($judul, $penerbit)
  35.         {
  36.             $sql = "INSERT INTO buku(buku, penerbit) VALUES('$judul', '$penerbit')";
  37.             $query = $this->db->query($sql);
  38.  
  39.             if (!$query) {
  40.                 return "failed!";
  41.             }else{
  42.                 return "success!";
  43.             }
  44.         }
  45.  
  46.         public function showBuku()
  47.         {
  48.             $sql = "SELECT * FROM buku";
  49.             $query = $this->db->query($sql);
  50.             return $query;
  51.         }
  52.  
  53.         public function editBuku($id)
  54.         {
  55.             $sql = "SELECT * FROM buku WHERE id = '$id'";
  56.             $query = $this->db->query($sql);
  57.             return $query;
  58.         }
  59.  
  60.         public function updateBuku($id, $judul, $penerbit)
  61.         {
  62.             $sql = "UPDATE buku SET buku = '$judul', penerbit = '$penerbit' WHERE id = '$id'";
  63.             $query = $this->db->query($sql);
  64.             if (!$query) {
  65.                 return "failed!";
  66.             }else{
  67.                 return "success!";
  68.             }
  69.         }
  70.  
  71.     }
  72.  
  73. #edit.php
  74.  
  75. <?php include_once('includes/header.php'); ?>
  76. <div class="row">
  77.     <div class="grid">
  78.         <div class="col-12">
  79.             <div class="col">
  80.                 <?php
  81.                     require 'library.php';
  82.                     if (isset($_GET['kode'])) {
  83.  
  84.                         $buku = new Library();
  85.                         $book = $buku->editBuku($_GET['kode']);
  86.                         $row = $book->fetch(PDO::FETCH_OBJ);
  87.                     }
  88.                 ?>
  89.                 <form action="edit_action.php" method="POST">
  90.                     <label for="fname">Judul Buku</label>
  91.                     <input type="text" id="fname" name="judul" value="<?php echo $row->buku; ?>">
  92.                     <label for="lname">Penerbit</label>
  93.                     <input type="text" id="lname" name="penerbit" value="<?php echo $row->penerbit; ?>">
  94.                     <input type="submit" value="Submit" name="submit">
  95.                     <?php echo $row->id; ?>
  96.                 </form>
  97.             </div>
  98.         </div>
  99.     </div>
  100. </div>
  101. <?php include_once('includes/footer.php'); ?>
Advertisement
Add Comment
Please, Sign In to add comment