Contoh Aplikasi CRUD Php MySql Dengan PDO
Bagi yang belum tau apa iti PDO bisa Baca Disini,
Oke langsung ke kodingan
1. kode untuk mesin_tabel.php
<form action="mesin_input.php"/>
<body>
<?php
include 'koneksi.php';
?>
<style>
tbody > tr:nth-child(2n+1)>td, tbody > tr:nth-child(2n+1)>th{
background-color:#eeeeee;
}
table{
width:70%;
margin:auto;
border-collapse:collapse;
box-shadow:darkgrey 3px;
}
thead tr{
background-color:#B7B7B7;
}
</style>
<h1 align="center" >Tabel Data Mesin </h1>
<table align="center">
<tr><td><input type="submit" value="Tambah Data"/></td></tr>
</table>
<table border="1">
<thead>
<tr>
<th>No</th>
<th>Kode Mesin</th>
<th>Merk</th>
<th>No Seri</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$sql ="Select * from tb_mesin";
$no = 1;
foreach ($dbh->query($sql) as $data):
?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $data['kd_mesin']?></td>
<td><?php echo $data['merk']?></td>
<td><?php echo $data['no_seri']?></td>
<td align="center">
<a href="mesin_edit.php?id=<?php echo $data['kd_mesin']?>">Edit</a> ||
<a href="mesin_hapus.php?id=<?php echo $data['kd_mesin']?>" onclick="return confirm('Anda Yakin Akan Menghapus data?')" >Hapus</a>
</td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
2. Kode untuk mesin_input.php
<title>Input data Mesin</title>
<style type="text/css">
body{
margin:auto;
background-color:#CCCCCC;
}
.lbl{
font:Verdana, Arial, Helvetica, sans-serif;
display:block;
}
.error{
font-size:16px;
color:#FF0000;
}
</style>
</head>
<form action="mesin_proses_simpan.php" method="post">
<body>
<table align="center" cellpadding="5" cellspacing="5" bordercolor="#006633">
<h1 align="center"> Input Data Mesin </h1>
<tr>
<td><label class="lbl">Kode Mesin </label><td> <input type="text" name="txtKd_mesin" maxlength="10" required /></td></td>
</tr>
<tr>
<td><label class="lbl">Merk Mesin </label><td><input type="text" name="txtMerk" maxlength="40" size="40" required /></td></td>
</tr>
<tr>
<td><label class="lbl">No Seri</label><td><input type="text" name="txtNoseri" maxlength="10" required /></td></td>
</tr>
<tr>
<td><input type="submit" value="Simpan"/><td><input type="reset" value="Reset" onclick="return confirm('Hapus data yang telah di input')" /></td></td>
</tr>
</table>
</body>
</html>
3. Kode untuk mesin_proses_simpan.php
<body>
<?php
include 'koneksi.php';
$data = array
(
$vkd_mesin = $_POST['txtKd_mesin'],
$vmerk = $_POST['txtMerk'],
$vnoseri = $_POST['txtNoseri'],
);
if (isset($_POST)){
$sql = "Insert into tb_mesin(kd_mesin, merk, no_seri) values ('$vkd_mesin','$vmerk','$vnoseri')";
$dbh->exec($sql);
}
header("location:mesin_tabel.php");
?>
</body>
4. kode Untuk mesin_hapus.php
<?php
include 'koneksi.php';
$vkd_mesin = $_GET['id'];
if (isset($vkd_mesin)){
$dbh->exec("Delete from tb_mesin where kd_mesin = '$vkd_mesin' ");
}
header ("location:mesin_tabel.php");
?>
5. kode untuk mesin_edit.php
<body>
<?php
include "koneksi.php";
if (isset($_GET['id'])){
$query = $dbh ->query("select * from tb_mesin where kd_mesin = '$_GET[id]'");
$data = $query ->fetch (PDO::FETCH_ASSOC);
}else{
echo "Kode Tidak Dikenal!";
}
if($data===false){
echo "Data Tidak Ditemukan";
}
?>
<h1 align="center">Edit Data</h1>
<fieldset style="width:50%; margin:auto;">
<legend>Form Edit Data Mesin</legend>
<form action="mesin_update.php" method="post">
<input type="hidden" name="id" disabled="disabled" value="<?php echo $data['id']; ?>" />
<p> Kode Mesin <input type="text" name="kd_mesin" required value="<?php echo $data['kd_mesin'];?>" /></p>
<p> Merk <input type="text" name="merk" required value="<?php echo $data['merk'];?>" /> </p>
<p> No Seri <input type="text" name="no_seri" required value="<?php echo $data ['no_seri'];?>" /></p>
<p><input type="submit" value="Update" />
</body>
6. Kode untuk Mesin_update.php
<body>
<?php
include "koneksi.php";
if (isset($_POST)){
$sql = "Update tb_mesin set merk = '$_POST[merk]', no_seri = '$_POST[no_seri]' where kd_mesin = '$_POST[kd_mesin]' " ;
$dbh ->exec($sql);
}
header ("location:mesin_tabel.php");
?>
</body>
7. Koneksi nya dg nama koneksi.php
<body>
<?php
$dsn = "mysql:dbname=db_pam; host :localhost";
$user = "root";
$pass = "";
try {
$dbh = new PDO($dsn,$user,$pass);
}catch (PDOException $e) {
echo "Koneksi gagal : ".$e->getMessage();
}
?>
</body>
Download Projek lengkapnya Di Sini
Oke langsung ke kodingan
1. kode untuk mesin_tabel.php
<form action="mesin_input.php"/>
<body>
<?php
include 'koneksi.php';
?>
<style>
tbody > tr:nth-child(2n+1)>td, tbody > tr:nth-child(2n+1)>th{
background-color:#eeeeee;
}
table{
width:70%;
margin:auto;
border-collapse:collapse;
box-shadow:darkgrey 3px;
}
thead tr{
background-color:#B7B7B7;
}
</style>
<h1 align="center" >Tabel Data Mesin </h1>
<table align="center">
<tr><td><input type="submit" value="Tambah Data"/></td></tr>
</table>
<table border="1">
<thead>
<tr>
<th>No</th>
<th>Kode Mesin</th>
<th>Merk</th>
<th>No Seri</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$sql ="Select * from tb_mesin";
$no = 1;
foreach ($dbh->query($sql) as $data):
?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $data['kd_mesin']?></td>
<td><?php echo $data['merk']?></td>
<td><?php echo $data['no_seri']?></td>
<td align="center">
<a href="mesin_edit.php?id=<?php echo $data['kd_mesin']?>">Edit</a> ||
<a href="mesin_hapus.php?id=<?php echo $data['kd_mesin']?>" onclick="return confirm('Anda Yakin Akan Menghapus data?')" >Hapus</a>
</td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
2. Kode untuk mesin_input.php
<title>Input data Mesin</title>
<style type="text/css">
body{
margin:auto;
background-color:#CCCCCC;
}
.lbl{
font:Verdana, Arial, Helvetica, sans-serif;
display:block;
}
.error{
font-size:16px;
color:#FF0000;
}
</style>
</head>
<form action="mesin_proses_simpan.php" method="post">
<body>
<table align="center" cellpadding="5" cellspacing="5" bordercolor="#006633">
<h1 align="center"> Input Data Mesin </h1>
<tr>
<td><label class="lbl">Kode Mesin </label><td> <input type="text" name="txtKd_mesin" maxlength="10" required /></td></td>
</tr>
<tr>
<td><label class="lbl">Merk Mesin </label><td><input type="text" name="txtMerk" maxlength="40" size="40" required /></td></td>
</tr>
<tr>
<td><label class="lbl">No Seri</label><td><input type="text" name="txtNoseri" maxlength="10" required /></td></td>
</tr>
<tr>
<td><input type="submit" value="Simpan"/><td><input type="reset" value="Reset" onclick="return confirm('Hapus data yang telah di input')" /></td></td>
</tr>
</table>
</body>
</html>
3. Kode untuk mesin_proses_simpan.php
<body>
<?php
include 'koneksi.php';
$data = array
(
$vkd_mesin = $_POST['txtKd_mesin'],
$vmerk = $_POST['txtMerk'],
$vnoseri = $_POST['txtNoseri'],
);
if (isset($_POST)){
$sql = "Insert into tb_mesin(kd_mesin, merk, no_seri) values ('$vkd_mesin','$vmerk','$vnoseri')";
$dbh->exec($sql);
}
header("location:mesin_tabel.php");
?>
</body>
4. kode Untuk mesin_hapus.php
<?php
include 'koneksi.php';
$vkd_mesin = $_GET['id'];
if (isset($vkd_mesin)){
$dbh->exec("Delete from tb_mesin where kd_mesin = '$vkd_mesin' ");
}
header ("location:mesin_tabel.php");
?>
5. kode untuk mesin_edit.php
<body>
<?php
include "koneksi.php";
if (isset($_GET['id'])){
$query = $dbh ->query("select * from tb_mesin where kd_mesin = '$_GET[id]'");
$data = $query ->fetch (PDO::FETCH_ASSOC);
}else{
echo "Kode Tidak Dikenal!";
}
if($data===false){
echo "Data Tidak Ditemukan";
}
?>
<h1 align="center">Edit Data</h1>
<fieldset style="width:50%; margin:auto;">
<legend>Form Edit Data Mesin</legend>
<form action="mesin_update.php" method="post">
<input type="hidden" name="id" disabled="disabled" value="<?php echo $data['id']; ?>" />
<p> Kode Mesin <input type="text" name="kd_mesin" required value="<?php echo $data['kd_mesin'];?>" /></p>
<p> Merk <input type="text" name="merk" required value="<?php echo $data['merk'];?>" /> </p>
<p> No Seri <input type="text" name="no_seri" required value="<?php echo $data ['no_seri'];?>" /></p>
<p><input type="submit" value="Update" />
</body>
6. Kode untuk Mesin_update.php
<body>
<?php
include "koneksi.php";
if (isset($_POST)){
$sql = "Update tb_mesin set merk = '$_POST[merk]', no_seri = '$_POST[no_seri]' where kd_mesin = '$_POST[kd_mesin]' " ;
$dbh ->exec($sql);
}
header ("location:mesin_tabel.php");
?>
</body>
7. Koneksi nya dg nama koneksi.php
<body>
<?php
$dsn = "mysql:dbname=db_pam; host :localhost";
$user = "root";
$pass = "";
try {
$dbh = new PDO($dsn,$user,$pass);
}catch (PDOException $e) {
echo "Koneksi gagal : ".$e->getMessage();
}
?>
</body>
Download Projek lengkapnya Di Sini
Comments
Post a Comment