Halo gaes, pada kesempatan kali ini saya akan berbagi tutorial tentang cara membuat aplikasi CRUD sederhana menggunakan PHP dan MySQL dengan mysql.
Sebelum membahas langkah-langkahnya ada baiknya kita mengenal apa itu aplikasi CRUD?
CRUD adalah akronim untuk Create, Read, Update, dan Delete. Operasi CRUD adalah manipulasi data dasar untuk database. Dalam tutorial ini kita akan membuat aplikasi PHP sederhana untuk melakukan semua operasi ini pada tabel database MySQL di satu tempat.
Kita akan membuat sebuah aplikasi dengan tampilan seperti pada gambar di bawah dengan sebuah tabel yang menampilkan data dari database di sisi frontend. Terdapat beberapa tombol untuk mengubah data yang ada di database lewat antarmuka yang kita buat. Tabel pengguna yang kita buat akan berisi informasi pengguna seperti nama, username, password, email.
Tutorial ini saya tujukan terutama bagi teman-teman yang mungkin baru dalam mempelajari PHP. Memahami konsep CRUD sangat penting jika kita ingin membuat sebuah aplikasi. Tanpa memahami konsep ini, kita tidak bisa membuat aplikasi bahkan yang sangat sederhana sekalipun, kecuali jika aplikasi yang kita buat tidak berhubungan dengan database.
Bagi teman-teman yang belum menguasai dasar HTML, PHP dan MySQL, saya sarankan untuk mempelajarinya terlebih dahulu sebelum mengikuti tutorial ini. Akan lebih baik lagi jika Anda juga menguasai CSS. Karena di sini saya tidak akan menjelaskan hal-hal yang bersifat dasar. Jika teman-teman mengikuti tutorial ini, saya asumsikan teman-teman sudah menguasai dasar-dasar HTML, PHP dan MySQL.
Tampilan Edit Data
Tampilan Tambah Data
Langkah-Langkah Pembuatan CRUD
1.Install xampp di pc/laptop anda , kalau sudah nyalakan apache dan mysql di xampp control panel
2.Kemudian buka https://localhost/phpmyadmin di browser anda
isikan data table seperti contoh gambar berikut :
setelah database dibuat berikutnya adalah membuat koneksi mysql ke html, kalian bisa menggunakan tool apapun untuk coding, seperti notepad ++, visual studio code, dll.
3. Membuat Database.php
CREATE TABLE `user` (
`nama` int(11) NOT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Pertama kita buat terlebih dahulu file database.sql kemudian kita impor file 'database.php' ini ke dalam database (menggunakan PHPMyAdmin).
2. Membuat file config.php
<?php
class database{
var $host = "localhost";
var $uname = "root";
var $pass = "";
var $db = "test";
function __construct(){
$mysqli = new mysqli($this->host, $this->uname, $this->pass, $this->db);
}
function tampil_data(){
$mysqli = new mysqli($this->host, $this->uname, $this->pass, $this->db);
$data = $mysqli->query("select * from user");
while($d = $data->fetch_assoc()){
$hasil[] = $d;
}
return $hasil;
}
function input($nama,$alamat,$usia,$email){
$mysqli = new mysqli($this->host, $this->uname, $this->pass, $this->db);
$mysqli->query("insert into user values('','$nama','$username,'$password','$email')");
}
function hapus($id){
$mysqli = new mysqli($this->host, $this->uname, $this->pass, $this->db);
$mysqli->query("delete from user where id='$id'");
}
function edit($id){
$mysqli = new mysqli($this->host, $this->uname, $this->pass, $this->db);
$data = $mysqli->query("select * from user where id='$id'");
while($d = $data->fetch_assoc()){
$hasil[] = $d;
}
return $hasil;
}
function update($id,$nama,$alamat,$usia,$email){
$mysqli = new mysqli($this->host, $this->uname, $this->pass, $this->db);
$mysqli->query("update user set nama='$nama', username='$username', password='$password', email='$email' where id='$id'");
}
}
?>
File config.php menyimpan informasi tentang database host, username dan password. Sebagian besar server lokal bekerja dengan detail yang diberikan. Anda dapat mengubahnya sesuai dengan detail host dan database Anda.Untuk menghubungkan PHP dengan MySQL, kita menggunakan fungsi
mysqli_connect() dengan alamat server sebagai parameter pertama, user database sebagai parameter kedua, password user sebagai parameter ketiga, dan nama database sebagai parameter keempat.3. Membuat file index.php
<?php
include 'database.php';
$db = new database();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<title>Crud</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
margin: 0px auto;
padding: 10px;
}
th{
padding: 10px;
text-align: center;
background-color: #f1f1c1;
}
</style>
</head>
<body>
<h5 style="vertical-align=midle; position:relative; text-align:center; font-size:50px; color:black;"><i class ="material-icons" style="
vertical-align=midle; position:relative; text-align:center; font-size:70px; color:pink; ">bubble_chart</i>CRUD APP</h5>
<a href="input.php" title="Tambah Data" class="btn-floating btn-large waves-effect waves-light red" >
<i class="material-icons" style="position:relative; left:28%; font-size:60px; color:#FFC0CB ;">add_circle</i></a>
<table style="position:relative;" class="highlight">
<thead>
<tr>
<th data-field="id">No</th>
<th data-field="id">Nama</th>
<th data-field="name">Username</th>
<th data-field="price">Email</th>
<th data-field="price">Password</th>
<th data-field="price">Tool</th>
</tr>
</thead>
<?php
$no = 1;
foreach($db->tampil_data() as $data){
?>
<tbody>
<tr>
<td style="color:#000;"><?php echo $no++; ?></td>
<td style="color:#000;"><?php echo $data['nama']; ?></td>
<td style="color:#000;"><?php echo $data['username']; ?></td>
<td style="color:#000;"><?php echo $data['password']; ?></td>
<td style="color:#000;"><?php echo $data['email']; ?></td>
<td>
<a href="edit.php?id=<?php echo $data['id']; ?>&aksi=edit" class="btn-floating yellow darken-2" title="edit data"><i class="material-icons"
style="font-size:25px; color:#000;">edit</i></a>
<a href="proses.php?id=<?php echo $data['id']; ?>&aksi=hapus" class="btn-floating green" title="hapus data"><i class="material-icons"
style="font-size:25px; color:black;">delete</i></a>
</td>
</tr>
</tbody>
<?php
}
?>
</table>
</body>
</html>
File index.php merupakan file utama yang menyertakan file konfigurasi untuk koneksi database. Kemudian menampilkan semua daftar pengguna menggunakan MySQL Select Query. Pengguna yang akan ditampilkan di dalam daftar perlu menambahkan terlebih dahulu menggunakan tautan 'Tambahkan Pengguna Baru'.
4. Membuat file edit.php
<?php
include 'database.php';
$db = new database();
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="wahyuningsih amalia" />
<title>Edit Data</title>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/>
<style>
button{
position: absolute;
text-align: center;
transform: translate(-50%,-50%);
width: 200px;
height: 60px;
line-height: 60px;
color: #fff;
font-size:24px;
text-transform: uppercase;
text-decoration: none;
font-family: sans-serif;
box-sizing: border-box;
background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
background-size: 400%;
border-radius: 30px;
left:55%;
}
button:hover
{
animation: animate 8s linear infinite;
}
@keyframes animate
{
0%
{
background-position: 0%;
}
100%
{
background-position: 400%;
}
}
button:before
{
content:'';
position: absolute;
top: -5px;
left: -5px;
right: -5px;
bottom: -5px;
z-index: -1;
background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
background-size: 400%;
border-radius: 40px;
opacity:0;
transition: 0.5s;
}
button:hover:before
{
filter: blur(20px);
opacity:1;
animation: animate 8s linear infinite;
}
</style>
<script>
function testInput(event) {
var value = String.fromCharCode(event.which);
var pattern = new RegExp("[a-zA-Z \b]");
return pattern.test(value);
}
function nospecial(event) {
var value = String.fromCharCode(event.which);
var pattern = new RegExp("[a-zA-Z0-9\b]");
return pattern.test(value);
}
</script>
</head>
<body>
<section>
<!-- <div class ="set">
<div><img src="image/1.png" width="150" height="150" ></div>
<div><img src="image/2.png" width="150" height="150"></div>
<div><img src="image/3.png" width="150" height="150"></div>
<div><img src="image/4.png" width="150" height="150"></div>
<div><img src="image/1.png" width="150" height="150"></div>
<div><img src="image/2.png" width="150" height="150"></div>
<div><img src="image/3.png" width="150" height="150"></div>
<div><img src="image/4.png" width="150" height="150"></div>
</div> -->
<h5 style="vertical-align=midle; position:relative; text-align:center; font-size:50px; color:#000;"><i class ="material-icons" style="
vertical-align=midle; position:relative; text-align:center; font-size:70px; color:#000; ">bubble_chart</i>Edit Data</h5>
<form style="color:purple; font-size:25px; text-align:center; padding:3px; " action="proses.php?aksi=update" method="post">
<?php
foreach($db->edit($_GET['id']) as $data){
?>
<div class="input-field">
<span><i style="font-size:30px;" class="material-icons">person</i> Nama</span>
<input style="position:relative;left:10px; width:30%; height:40px; padding:5px;" id="icon_prefix" type="hidden" name="id" value="<?php echo $data['id']; ?>" id="id" class="validate">
<input style="position:relative;left:10px; width:30%; height:40px; padding:5px;" id="icon_prefix" type="text" name="nama" onkeydown="return testInput(event);" required="required" id="nama" value="<?php echo $data['nama']; ?>" class="validate">
</div>
<div class="input-field">
<span><i style="font-size:30px;" class="material-icons">laptop</i> ID</span>
<input style="position:relative;left:32px; width:30%; height:40px; padding:5px;" id="icon_telephone" type="text" name="alamat" onkeydown="return nospecial(event);" required="required" id="alamat" value="<?php echo $data['alamat']; ?>" class="validate">
</div>
<div class="input-field">
<span ><i style="font-size:30px;" class="material-icons">email</i> Email</span>
<input style="position:relative; left:18px; width:30%; height:40px; padding:5px;" id="icon_telephone" type="email" name="usia" id="usia" required="required" value="<?php echo $data['usia']; ?>" class="validate">
</div>
<div class="input-field">
<span ><i style="font-size:30px;" class="material-icons">vpn_key</i> Password</span>
<input style="position:relative; width:30%; height:40px; padding:5px;" id="icon_telephone" type="password" name="email" id="email" required="required" value="<?php echo $data['email']; ?>" class="validate">
</div>
<div class="input-field">
<br>
<br>
<button type="submit">Edit</button>
</div>
<?php } ?>
</form>
</section>
</body>
</html>
Edit.php digunakan untuk mengedit / update data pengguna. Anda dapat mengubah data pengguna dan memperbaruinya. File ini akan mengarahkan pengguna kembali ke homepage, setelah update sukses.
4. Membuat file input.php
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="wahyuningsih amalia" />
<title>Input Data</title>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/>
<style>
button{
position: absolute;
text-align: center;
transform: translate(-50%,-50%);
width: 200px;
height: 60px;
line-height: 60px;
color: #fff;
font-size:24px;
text-transform: uppercase;
text-decoration: none;
font-family: sans-serif;
box-sizing: border-box;
background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
background-size: 400%;
border-radius: 30px;
left:55%;
}
button:hover
{
animation: animate 8s linear infinite;
}
@keyframes animate
{
0%
{
background-position: 0%;
}
100%
{
background-position: 400%;
}
}
button:before
{
content:'';
position: absolute;
top: -5px;
left: -5px;
right: -5px;
bottom: -5px;
z-index: -1;
background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
background-size: 400%;
border-radius: 40px;
opacity:0;
transition: 0.5s;
}
button:hover:before
{
filter: blur(20px);
opacity:1;
animation: animate 8s linear infinite;
}
</style>
<script>
function testInput(event) {
var value = String.fromCharCode(event.which);
var pattern = new RegExp("[a-zA-Z \b]");
return pattern.test(value);
}
function nospecial(event) {
var value = String.fromCharCode(event.which);
var pattern = new RegExp("[a-zA-Z0-9\b]");
return pattern.test(value);
}
</script>
</head>
<body>
<section>
<!-- <div class ="set">
<div><img src="image/1.png" width="150" height="150" ></div>
<div><img src="image/2.png" width="150" height="150"></div>
<div><img src="image/3.png" width="150" height="150"></div>
<div><img src="image/4.png" width="150" height="150"></div>
<div><img src="image/1.png" width="150" height="150"></div>
<div><img src="image/2.png" width="150" height="150"></div>
<div><img src="image/3.png" width="150" height="150"></div>
<div><img src="image/4.png" width="150" height="150"></div>
</div> -->
<h5 style="vertical-align=midle; position:relative; text-align:center; font-size:50px; color:#000;"><i class ="material-icons" style="
vertical-align=midle; position:relative; text-align:center; font-size:70px; color:purple; ">bubble_chart</i>tambah data</h5>
<br/>
<form style="color:#000; font-size:25px; text-align:center; padding:3px; " action="proses.php?aksi=tambah" method="post">
<div class="input-field">
<span><i style="font-size:30px;" class="material-icons">person</i> Nama</span>
<input style="position:relative;left:10px; width:30%; height:40px; padding:5px;" id="icon_prefix" type="text" required="required" name="nama" id="nama" class="validate"
onkeydown="return testInput(event);" >
</div>
<div class="input-field">
<span><i style="font-size:30px;" class="material-icons">laptop</i>Username</span>
<input style="position:relative;left:32px; width:30%; height:40px; padding:5px;" id="icon_telephone" type="text" required="required" name="alamat" id="alamat"
class="validate" onkeydown="return nospecial(event);">
</div>
<div class="input-field">
<span ><i style="font-size:30px;" class="material-icons">email</i> Email</span>
<input style="position:relative; left:18px; width:30%; height:40px; padding:5px;" id="icon_telephone" type="email" required="required" name="usia" id="usia" class="validate">
</div>
<div class="input-field">
<span ><i style="font-size:30px;" class="material-icons">vpn_key</i> Password</span>
<input style="position:relative; width:30%; height:40px; padding:5px;" id="icon_telephone" type="password" required="required" name="email" id="email" class="validate">
</div>
<div class="input-field">
<br>
<br>
<button type="submit">simpan</button>
</div>
</form>
</section>
</body>
</html>
Pada tahap ini kita akan membuat dua buah file. File yang pertama berisi sebuah form untuk memasukkan data yang akan kita beri nama input.php. File yang kedua berfungsi untuk memproses data yang telah dimasukkan pada form input.php. File ini kita beri nama input_proses.php.
Berikut ini adalah tampilan kode pada file input.php:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="wahyuningsih amalia" />
<title>Input Data</title>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/>
<style>
button{
position: absolute;
text-align: center;
transform: translate(-50%,-50%);
width: 200px;
height: 60px;
line-height: 60px;
color: #fff;
font-size:24px;
text-transform: uppercase;
text-decoration: none;
font-family: sans-serif;
box-sizing: border-box;
background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
background-size: 400%;
border-radius: 30px;
left:55%;
}
button:hover
{
animation: animate 8s linear infinite;
}
@keyframes animate
{
0%
{
background-position: 0%;
}
100%
{
background-position: 400%;
}
}
button:before
{
content:'';
position: absolute;
top: -5px;
left: -5px;
right: -5px;
bottom: -5px;
z-index: -1;
background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
background-size: 400%;
border-radius: 40px;
opacity:0;
transition: 0.5s;
}
button:hover:before
{
filter: blur(20px);
opacity:1;
animation: animate 8s linear infinite;
}
</style>
<script>
function testInput(event) {
var value = String.fromCharCode(event.which);
var pattern = new RegExp("[a-zA-Z \b]");
return pattern.test(value);
}
function nospecial(event) {
var value = String.fromCharCode(event.which);
var pattern = new RegExp("[a-zA-Z0-9\b]");
return pattern.test(value);
}
</script>
</head>
<body>
<section>
<!-- <div class ="set">
<div><img src="image/1.png" width="150" height="150" ></div>
<div><img src="image/2.png" width="150" height="150"></div>
<div><img src="image/3.png" width="150" height="150"></div>
<div><img src="image/4.png" width="150" height="150"></div>
<div><img src="image/1.png" width="150" height="150"></div>
<div><img src="image/2.png" width="150" height="150"></div>
<div><img src="image/3.png" width="150" height="150"></div>
<div><img src="image/4.png" width="150" height="150"></div>
</div> -->
<h5 style="vertical-align=midle; position:relative; text-align:center; font-size:50px; color:#000;"><i class ="material-icons" style="
vertical-align=midle; position:relative; text-align:center; font-size:70px; color:purple; ">bubble_chart</i>tambah data</h5>
<br/>
<form style="color:#000; font-size:25px; text-align:center; padding:3px; " action="proses.php?aksi=tambah" method="post">
<div class="input-field">
<span><i style="font-size:30px;" class="material-icons">person</i> Nama</span>
<input style="position:relative;left:10px; width:30%; height:40px; padding:5px;" id="icon_prefix" type="text" required="required" name="nama" id="nama" class="validate"
onkeydown="return testInput(event);" >
</div>
<div class="input-field">
<span><i style="font-size:30px;" class="material-icons">laptop</i>Username</span>
<input style="position:relative;left:32px; width:30%; height:40px; padding:5px;" id="icon_telephone" type="text" required="required" name="alamat" id="alamat"
class="validate" onkeydown="return nospecial(event);">
</div>
<div class="input-field">
<span ><i style="font-size:30px;" class="material-icons">email</i> Email</span>
<input style="position:relative; left:18px; width:30%; height:40px; padding:5px;" id="icon_telephone" type="email" required="required" name="usia" id="usia" class="validate">
</div>
<div class="input-field">
<span ><i style="font-size:30px;" class="material-icons">vpn_key</i> Password</span>
<input style="position:relative; width:30%; height:40px; padding:5px;" id="icon_telephone" type="password" required="required" name="email" id="email" class="validate">
</div>
<div class="input-field">
<br>
<br>
<button type="submit">simpan</button>
</div>
</form>
</section>
</body>
</html>
Tampilan kode input_proses.php adalah sebagai berikut:
<?php
include 'database.php';
$db = new database();
$aksi = $_GET['aksi'];
if($aksi == "tambah"){
$db->input($_POST['nama'],$_POST['alamat'],$_POST['usia'],$_POST['email']);
header("location:index.php");
}elseif($aksi == "hapus"){
$db->hapus($_GET['id']);
header("location:index.php");
}elseif($aksi == "update"){
$db->update($_POST['id'],$_POST['nama'],$_POST['alamat'],$_POST['usia'],$_POST['email']);
header("location:index.php");
}
?>
Sampai tahap ini kita telah selesai membuat aplikasi CRUD sederhana untuk mengolah data mahasiswa.
Demikian tutorial Membuat Aplikasi CRUD Sederhana Menggunakan PHP dan Database MySQL yang saya buat. Terima kasih. Semoga bermanfaat :)




