Rabu, 28 Desember 2016

Belajar CRUD dengan Menggunakan PHP

Assalamu'alaikum Wr.Wb

Hi semua, kali ini saya akan menjelaskan sedikit tentang belajar CRUD (Create, Read, Update, dan Delete) dengan menggunakan PHP.
Untuk contoh yang telah saya buat, berikut tampilan CRUD yang ada dalam program tersebut.

Untuk membuat seperti diatas, berikut langkah-langkah yang harus dilakukan :

1. Buat database dengan nama latihan1.sql


2. Buat CRUD dengan menggunakan PHP. Disini kita bisa membuat script php yaitu config.php, edit.php, hapus.php, index.php, proses.php, tambah.php, tampil.php.

** Berikut script config.php
<?php
mysql_connect('localhost','root','') or die (mysql_error());
mysql_select_db('latihan1') or die (mysql_error());
?>

** tambah.php
<?php

if(isset($_POST['tambah'])){
$nama = @$_POST['nama'];
$username = @$_POST['username'];
$password = @$_POST['password'];
$email = @$_POST['email'];
$hasil=mysql_query("insert into crud values ('','$nama','$username','$password','$email')");
if ($hasil) {
?>
<script type="text/javascript">
alert("Data Telah Ditambahkan!");
window.location.href="?page=";
</script>
<?php
} else {
echo mysql_error();
}
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Tambah</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1><center>Tambah Data</h1><br/><br/>
<form action="" method="post">
<table class="center_table">
<tr>
<td width="30">Nama</td>
<td width="20">:</td>
<td width="300"><input type="text" name="nama" class="in" pattern="[a-zA-Z ]+" placeholder="Masukkan Nama" required
oninvalid="this.setCustomValidity('Input hanya boleh huruf a-z dan spasi')"></td>
</tr>
<tr>
<td>Username</td>
<td>:</td>
<td><input type="text" name="username" class="in" pattern="[a-zA-Z ]+" placeholder="Masukkan username" required
oninvalid="this.setCustomValidity('Input hanya boleh huruf a-z dan spasi')"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="password" name="password" class="in" placeholder="Masukkan Password" required></td>
</tr>
<tr>
<td>E-mail</td>
<td>:</td>
<td><input type="text" name="email" class="in" placeholder="Masukkan E-mail" required></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="tambah" value="Tambah Data" class="btn" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>

** tampil.php
<br />
<table border="1px">
<tr>
<th>No</th>
<th>Nama</th>
<th>Username</th>
<th>E-mail</th>
<th colspan="2">Keterangan</th>
</tr>
<?php
include "config.php"; // agar bisa connect ke database

$hasil=mysql_query("select * from crud"); // memilih semua data dari tabel user
$no = 1; //membuat no urut
while ($data=mysql_fetch_array($hasil)) { // pengulangan 'while' agar semua data bisa tampil
?>
<tr align="center">
<td><?php echo $no; ?></td>
<td><?php echo $data['nama'] ?></td>
<td><?php echo $data['username'] ?></td>
<td><?php echo $data['email'] ?></td>
<td><a href="?page=&action=edit&id=<?php echo $data['id'] ?>"><button>Edit</button></a></td>
<td><a href="?page=&action=hapus&id=<?php echo $data['id'] ?>" onclick="return confirm('Apakah Anda ingin menghapus??')"><button>Hapus</button></a></td>
</tr>
<?php
$no++; //agar no bisa urut
} // akhir pengulangan while
?>
</table>
<br />
<a href="?page=&action=tambah"><center><button>Tambah Data</button></a>

** Berikut script untuk edit.php
<?php
$id = @$_GET['id'];
$sql = mysql_query("select * from crud where id = '$id'") or die(mysql_error()); //memilih data
$data = mysql_fetch_array($sql);
?>

<!DOCTYPE html>
<html>
<head>
<title>Tambah</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
if(isset($_POST['edit'])){
$nama = @$_POST['nama'];
$username = @$_POST['username'];
$password = @$_POST['password'];
$email = @$_POST['email'];
$hasil=mysql_query("update crud set nama = '$nama', username = '$username', password = '$password', email = '$email' where  id = '$id'");
if ($hasil) {
?>
<script type="text/javascript">
alert("Edit Data Berhasil");
window.location.href="?page=";
</script>
<?php
} else {
echo mysql_error();
}
}
?>
<h1><center>Edit Data</h1><br/><br/>

<form action="" method="post">
<table class="center_table">
<tr>
<td width="30">Nama</td>
<td width="20">:</td>
<td width="300"><input type="text" name="nama" class="in" value="<?php echo $data['nama']; ?>" /></td>
</tr>
<tr>
<td>Username</td>
<td>:</td>
<td><input type="text" name="username" class="in" value="<?php echo $data['username']; ?>" /></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="text" name="password" class="in" value="<?php echo $data['password']; ?>" /></td>
</tr>
<tr>
<td>E-mail</td>
<td>:</td>
<td><input type="text" name="email" class="in" value="<?php echo $data['email']; ?>"/></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="edit" value="Edit Data" class="btn" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>

** hapus.php
<?php
$id = @$_GET['id'];

mysql_query("delete from crud where id = '$id'") or die (mysql_error());
?>

<script type="text/javascript">
window.location.href="?page"
</script>

** index.php
<?php
include "config.php";
?>

<!DOCTYPE html>
<html>
<head>
<title>Crud dengan PHP</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="header">
<p> Belajar Crud dengan Menggunakan PHP</p>
</div>
<div class="isi">
<?php
$page = @$_GET['page'];
$action =@$_GET['action'];
if ($page == "") {
if($action == "") {
include "tampil.php";

} else if ($action == "tambah") {
include "tambah.php";
} else if ($action == "edit") {
include "edit.php";
} else if ($action == "hapus") {
include "hapus.php";
}
}
?>
</div>
<div class="fixedBar">
    <div class="boxfloat">
        </div>
</div>


</body>
</html>

** proses.php
<?php
if(isset($_GET['nama']) AND isset($_GET['email]))
{
echo $_GET['nama'];
}
echo "<br/>";
if(isset($_GET['username']))
{
echo $_GET['username'];
}
echo "<br/>";
if(isset($_GET['password']))
{
echo $_GET['password'];
}
echo "<br/>";
if(isset($_GET['email']))
{
echo $_GET['email'];
}
?>

Untuk script dibawah, bisa dilakukan apabila tampilan program CRUD yang kalian buat ingin dipercantik seperti pada gambar paling atas.

** style.css
*{
margin: 0;
padding: 0;
}

#header{
position: fixed;
height: 50px;
text-align:center;
width: 100%;

}

#header p{
color: #00CCFF;
line-height: 50px;
font-size: 35px;
color: #0066FF;
}
.isi{
clear: both;
padding-top: 70px;
padding-left: 30px;
padding-right: 30px;

}

button {
padding: 5px;
border-radius: 5px;
background-color: #00CCFF;
}

table{
border-collapse: collapse;
width: 100%;
border-radius: 5px;
}

th{
background-color: #00CCFF;
padding: 5px;
}

.in{

padding: 5px;
border: 2px #0066FF solid;

}

.btn{
padding: 5px;
background-color: #0066FF;
border-radius: 5px;
}
.center_table{
margin-left: 400px;
}

Sekian penjelasan singkat mengenai CRUD dengan menggunakan PHP. Semoga bermanfaat untuk yang menulis ataupun yang membacanya. Terimakasih :)

0 komentar:

Posting Komentar