Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #edit_action.php
- <?php
- require 'library.php';
- if (isset($_POST['submit'])) {
- $id = $_GET['kode'];
- $buku = $_POST['buku'];
- $penerbit = $_POST['penerbit'];
- $up = $buku->updateBuku($id, $judul, $penerbit);
- if ($up == 'Seccess') {
- header('Location:index.php');
- }else{
- echo "failed!";
- }
- }
- ?>
- #library.php
- <?php
- /**
- * library.php
- * connection, show, add, edit, delete!
- */
- class library{
- public function __construct()
- {
- $this->db = new PDO('mysql:host=localhost;dbname=crudbuku', 'root', '');
- }
- public function addBuku($judul, $penerbit)
- {
- $sql = "INSERT INTO buku(buku, penerbit) VALUES('$judul', '$penerbit')";
- $query = $this->db->query($sql);
- if (!$query) {
- return "failed!";
- }else{
- return "success!";
- }
- }
- public function showBuku()
- {
- $sql = "SELECT * FROM buku";
- $query = $this->db->query($sql);
- return $query;
- }
- public function editBuku($id)
- {
- $sql = "SELECT * FROM buku WHERE id = '$id'";
- $query = $this->db->query($sql);
- return $query;
- }
- public function updateBuku($id, $judul, $penerbit)
- {
- $sql = "UPDATE buku SET buku = '$judul', penerbit = '$penerbit' WHERE id = '$id'";
- $query = $this->db->query($sql);
- if (!$query) {
- return "failed!";
- }else{
- return "success!";
- }
- }
- }
- #edit.php
- <?php include_once('includes/header.php'); ?>
- <div class="row">
- <div class="grid">
- <div class="col-12">
- <div class="col">
- <?php
- require 'library.php';
- if (isset($_GET['kode'])) {
- $buku = new Library();
- $book = $buku->editBuku($_GET['kode']);
- $row = $book->fetch(PDO::FETCH_OBJ);
- }
- ?>
- <form action="edit_action.php" method="POST">
- <label for="fname">Judul Buku</label>
- <input type="text" id="fname" name="judul" value="<?php echo $row->buku; ?>">
- <label for="lname">Penerbit</label>
- <input type="text" id="lname" name="penerbit" value="<?php echo $row->penerbit; ?>">
- <input type="submit" value="Submit" name="submit">
- <?php echo $row->id; ?>
- </form>
- </div>
- </div>
- </div>
- </div>
- <?php include_once('includes/footer.php'); ?>
Advertisement
Add Comment
Please, Sign In to add comment