Anda di halaman 1dari 5

Membuat Aplikasi Input, Lihat, Edit dan Delete

Buat Database “perpustakaan” dengan nama Tabel “buku” dengan field

Membuat aplikasi dengan nama “tampil.php” dengan desain sebagai berikut

Sedangkan script yang dibutuhkan adalah sebagai berikut :

<?php
include('koneksi.php');
$db = new database();
$mydata = $db->tampil_data();
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1 align="center">Mari Berlatih OOP dengan Database</h1>
<h2 align="center">Smangat belajar, jangan pantang menyerah</h2>
<p align="center"><a href="tambahdata.php">Tambah Data</a>
</p>
<table width="1317" border="1" align="center" wkodebukuth="997">
<tr>
<th width="205" wkodebukuth="75">Kode Buku</th>
<th width="403" wkodebukuth="250">Judul Buku</th>
<th width="315" wkodebukuth="200">Penerbit</th>
<th width="357" wkodebukuth="200">Pengarang</th>
<th width="220" wkodebukuth="207">Pilihan</th>
</tr>
<?php
$no = 1;
foreach($mydata as $row){
?>
<tr>
<td><?php echo $row['kodebuku']; ?></td>
<td><?php echo $row['judulbuku']; ?></td>
<td><?php echo $row['pengarang']; ?></td>
<td><?php echo $row['penerbit']; ?></td>
<td>
<a href="edit.php?kodebuku=<?php echo $row['kodebuku']; ?>">Update</a>
<a href="proses_data.php?action=delete&kodebuku=<?php echo $row['kodebuku']; ?>">Delete</a>
</td>
</tr>
<?php
}
?>
</table>
</body>
</html>

Membuat aplikasi “koneksi.php”

<?php
class database{

var $host = "localhost";


var $username = "root";
var $password = "";
var $database = "perpustakaan";
var $koneksi = "";
function __construct(){
$this->koneksi = mysqli_connect($this->host, $this->username, $this->password,$this->database);
if (mysqli_connect_errno()){
echo "Koneksi database gagal : " . mysqli_connect_error();
}
}

function tampil_data() {
$data = mysqli_query($this->koneksi,"select * from buku");
while($row = mysqli_fetch_array($data)){
$hasil[] = $row;
}
return $hasil;
}

function tambah_data($kodebuku, $judulbuku, $pengarang, $penerbit) {


mysqli_query($this->koneksi,"insert into buku values ('$kodebuku','$judulbuku','$pengarang',
'$penerbit')");
}

function get_by_kodebuku($kodebuku) {
$query = mysqli_query($this->koneksi,"select * from buku where kodebuku='$kodebuku'");
return $query->fetch_array();
}
function update_data($kodebuku, $judulbuku, $pengarang, $penerbit, $kodebuku_asli) {
$query = mysqli_query($this->koneksi,"update buku set kodebuku='$kodebuku',
judulbuku='$judulbuku', pengarang='$pengarang', penerbit='$penerbit' where kodebuku='$kodebuku_asli'");

}
function delete_data($kodebuku_data) {
$query = mysqli_query($this->koneksi,"delete from buku where kodebuku='$kodebuku'");
}
}
?>

Membuat aplikasi dengan nama “proses-data.php”

<?php
include('koneksi.php');
$koneksi = new database();
$action = $_GET['action'];
if($action == "add") {
$koneksi-
>tambah_data($_POST['kodebuku'],$_POST['judulbuku'],$_POST['pengarang'],$_POST['penerbit']);
header('location:tampil.php');
} elseif ($action=="update") {
$koneksi->update_data($_POST['kodebuku'], $_POST['judulbuku'], $_POST['pengarang'],
$_POST['penerbit'], $_POST['kodebuku_asli']);
//echo " ini adalah : ".$_POST['kodebuku']." --- ".$_POST['judulbuku']." -- ".$_POST['pengarang']." --
".$_POST['kodebuku_asli']." - ";
header('location:tampil.php');
} elseif ($action=="delete") {
$kodebuku_data = $_GET['kodebuku'];
$koneksi->delete_data($kodebuku_data);
header('location:tampil.php');
}
?>

Membuat aplikasi “edit.php”

<?php
include('koneksi.php');
$db = new database();
$kodebuku = $_GET['kodebuku'];
$kodebuku_asli = $kodebuku;
if(! is_null($kodebuku)) {
$mydata = $db->get_by_kodebuku($kodebuku);
} else {
header('location:tampil.php');
}
//echo " ------------------- ".$kodebuku." -------------------".$kodebuku_asli;
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h3>Update Data User</h3>
<hr/>
<form method="post" action="proses_data.php?action=update">
<input type="hidden" name="kodebuku_asli" value="<?= $kodebuku_asli; ?>"/>
<table>
<tr>
<td>KODE BUKU</td>
<td>:</td>
<td><input type="text" name="kodebuku" value="<?php echo $mydata['kodebuku']; ?>"/></td>
</tr>
<tr>
<td>JUDUL BUKU</td>
<td>:</td>
<td><input type="text" name="judulbuku" value="<?php echo $mydata['judulbuku']; ?>"/></td>
</tr>
<tr>
<td>PENGARANG</td>
<td>:</td>
<td><input type="text" name="pengarang" value="<?php echo $mydata['pengarang']; ?>"/></td>
</tr>
<tr>
<td>PENERBIT</td>
<td>:</td>
<td><input type="text" name="penerbit" value="<?php echo $mydata['penerbit']; ?>"/></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="tombol" value="Update"/></td>
</tr>
</table>
</form>
</body>
</html>

Anda mungkin juga menyukai