♠ Posted by Unknown at 06.47
Penerapan CRUD Pada Form
Saya mencoba membuat Login dan CRUD sederhana dengan membuat form data guru dan menampilkannya pada tabel. Berikut ulasannya.
Koneksi.php
Halaman Login
login.php
Dari script di atas maka akan menghasilkan tampilan sebagai berikut.
Hasil dari CRUD akan saya tampilkan di home.
Pertama kita langsung membuat CREATE untuk menambahkan data.
tambah-data.php
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: ../login.php');
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tambah Data</title>
<meta charset="utf-8">
<link rel="stylesheet" href="asset/css/bootstrap.min.css">
<link rel="stylesheet" href="asset/css/bootstrap-theme.min.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<form method="post" action="proses-tambah-data.php">
<div class="form-group">
<h2 style="text-align: center;">Tambah Data Guru </h2>
<label for="text">Nama Lengkap</label>
<input type="text" class="form-control" name="nama" placeholder="Masukkan nama lengkap anda">
</div>
<div class="form-group">
<label for="text">Alamat</label>
<input type="text" class="form-control" name="alamat" placeholder="Masukkan alamat anda">
</div>
<div class="form-group">
<label for="text">Jenis Kelamin</label><br>
<label class="radio-inline"><input type="radio" name="jk" value="L">Laki-laki</label>
<label class="radio-inline"><input type="radio" name="jk" value="P">Perempuan</label>
</div>
<div class="form-group">
<label for="text">Pendidikan</label>
<input type="text" class="form-control" name="pendidikan" placeholder="Pendidikan terakhir anda">
</div>
<div class="form-group">
<label for="text">Status</label>
<input type="text" class="form-control" name="status" placeholder="Status anda">
</div>
<div class="form-group">
<label for="text">Telepon</label>
<input type="text" class="form-control" name="telepon" placeholder="Masukkan nomer telepon anda">
</div>
<button class="btn btn-danger">Simpan</button>
<button class="btn btn-danger" onclick="self.history.back();">Batal</button>
</form>
</body>
</html>
proses-tambah-data.php
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: ../login.php');
exit();
}
include 'koneksi.php';
$nama = $_POST['nama'];
$alamat = $_POST['alamat'];
$jk = $_POST['jk'];
$pendidikan = $_POST['pendidikan'];
$status = $_POST['status'];
$telepon = $_POST['telepon'];
$query = "INSERT INTO data (nama, alamat, jk, pendidikan, status, telepon) VALUES ('$nama', '$alamat', '$jk', '$pendidikan', '$status', '$telepon')";
$result = mysqli_query($db, $query);
if ($result== true) {
header('Location: home.php');
}else {
header('Location: tambah-data.php');
}
?>
Lalu membuat READ untuk halaman utama yang akan kita lihat.
home.php
<?php
include 'koneksi.php';
include 'proses-list-data.php';
$query = mysqli_query($db, 'SELECT * from data');
?>
<!DOCTYPE html>=
<html lang="en">
<head>
<title>Halaman Admin</title>
<meta charset="utf-8">
<link rel="stylesheet" href="asset/css/bootstrap.min.css">
<link rel="stylesheet" href="asset/css/bootstrap-theme.min.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<h2 style="text-align: center;">Data Guru Baru</h2>
<a href="tambah-data.php"><button class="btn btn btn-success" name="tambah">Tambah</button></a>
<br><br>
<table class="table table-bordered">
<?php if (empty($data_guru)) : ?>
Tidak ada data.
<?php else : ?>
<table class="data">
<tr>
<th>Nama</th>
<th>Alamat</th>
<th>JK</th>
<th>Pendidikan</th>
<th>Status</th>
<th>Telepon</th>
<th width="20%">Pilihan</th>
</tr>
<?php foreach ($data_guru as $data) : ?>
<tr>
<td><?php echo $data['nama'] ?></td>
<td><?php echo $data['alamat'] ?></td>
<td><?php echo $data['jk'] ?></td>
<td><?php echo $data['pendidikan'] ?></td>
<td><?php echo $data['status'] ?></td>
<td><?php echo $data['telepon'] ?></td>
<td>
<a href="edit-data.php?id_data=<?php echo $data['id_data']; ?>" class="btn btn-edit">Edit</a>
<a href="delete-data.php?id_data=<?php echo $data['id_data']; ?>" class="btn btn-hapus" onclick="return confirm('anda yakin akan menghapus data?');">Hapus</a>
</td>
</tr>
<?php endforeach ?>
</table>
<?php endif ?>
</table>
</body>
</html>
proses-list-data.php
<?php
if (session_status()!=PHP_SESSION_ACTIVE) {
session_start();
}
if (! isset($_SESSION['username'])) {
header('Location: ../login.php');
exit();
}
include 'koneksi.php';
$query = "SELECT * FROM data";
$hasil = mysqli_query($db, $query);
$data_guru = array();
while ($row = mysqli_fetch_assoc($hasil)) {
$data_guru[] = $row;
}
?>
Selanjutnya kita buat UPDATE untuk mengedit data.
edit-data.php
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: ../login.php');
exit();
}
include 'koneksi.php';
$id_data = $_GET['id_data'];
$query = "SELECT * FROM data WHERE id_data = $id_data";
$hasil = mysqli_query($db, $query);
$data_guru = mysqli_fetch_assoc($hasil);
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Data</title>
<meta charset="utf-8">
<link rel="stylesheet" href="asset/css/bootstrap.min.css">
<link rel="stylesheet" href="asset/css/bootstrap-theme.min.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<form method="post" action="proses-edit-data.php">
<div class="form-group">
<h2 style="text-align: center;">Edit Data Guru </h2>
<input type="hidden" name="id_data" value="<?php echo $data_guru['id_data']; ?>">
<label for="text">Nama Lengkap</label>
<input type="text" class="form-control" name="nama" value="<?php echo $data_guru['nama']; ?>">
</div>
<div class="form-group">
<label for="text">Alamat</label>
<input type="text" class="form-control" name="alamat" value="<?php echo $data_guru['alamat']; ?>"> </div>
<div class="form-group">
<label for="text">Jenis Kelamin</label><br>
<label class="radio-inline"><input type="radio" name="jk" value="<?php echo $data_guru['jk']; ?>">Laki-laki </label>
<label class="radio-inline"><input type="radio" name="jk" value="<?php echo $data_guru['jk']; ?>">Perempuan</label>
</div>
<div class="form-group">
<label for="text">Pendidikan</label>
<input type="text" class="form-control" name="pendidikan" value="<?php echo $data_guru['pendidikan']; ?>">
</div>
<div class="form-group">
<label for="text">Status</label>
<input type="text" class="form-control" name="status" value="<?php echo $data_guru['status']; ?>">
</div>
<div class="form-group">
<label for="text">Telepon</label>
<input type="text" class="form-control" name="telepon" value="<?php echo $data_guru['telepon']; ?>">
</div>
<button class="btn btn-danger">Simpan</button>
<button class="btn btn-danger" onclick="self.history.back();">Batal</button>
</form>
</body>
</html>
proses-edit-data.php
<?php
include 'koneksi.php';
include 'edit-data.php';
$id_data = $_POST['id_data'];
$nama = $_POST['nama'];
$alamat = $_POST['alamat'];
$jk = $_POST['jk'];
$pendidikan = $_POST['pendidikan'];
$status = $_POST['status'];
$telepon = $_POST['telepon'];
$query = "UPDATE data
SET nama = '$nama',
alamat = '$alamat',
jk = '$jk',
pendidikan = '$pendidikan',
status = '$status',
telepon = '$telepon'
WHERE id_data = '$id_data'";
$hasil = mysqli_query($db, $query);
if ($hasil == true) {
header('Location: home.php');
}else {
header('Location: tambah-data.php');
}
?>
Langkah selanjutnya membuat script untuk menghapus data yaitu DELETE.
delete-data.php
<?php
include 'koneksi.php';
$id_data = $_GET['id_data'];
$query = "DELETE FROM data WHERE id_data = $id_data";
$hasil = mysqli_query($db, $query);
if($hasil == true) {
header('Location: home.php');
}else {
header('Location: tambah-data.php');
}
?>
Berikut hasil dari script CRUD diatas.
home.php
tambah-data.php
home.php
edit-data.php
home.php
delete-data.php
0 komentar:
Posting Komentar