Tutorial PHP dan MySQL: Membuat Aplikasi CRUD [Studi Kasus Pendaftaran Siswa Baru] Lanjutan
Halo Kisanak, Kali ini aku menlanjutkan mengimplenetasikan Tutorial PHP & SQL yang dibuat oleh petanikode. Yang membedakan disini ialah tambahan input berupa Nomor Induk Siswa dan foto.
Referensi untuk CRUD dengan database yang ada fotonya bisa kalian liat mynotescode.com
Berikut ini repositori yang bisa kalian pakai diGithubku. Disini terdapat beberapa file php yang digunakan antara lain:
- config.php — untuk menyimpan konfigurasi database;
- index.php — halaman utama;
- list-siswa.php — halaman untuk menampilkan data siswa;
- form-daftar.php — halaman formulir pendaftaran;
- proses-pendaftaran.php — skrip yang memproses pendaftaran;
- form-edit.php — form untuk edit data siswa;
- proses-edit.php — skrip untuk memproses edit/update;
- hapus.php — skrip untuk menghapus data dari database;
Kalian bisa coba langsung disini dan begini tampilan yang berbeda:
Halaman Form Daftar
Halaman Form Edit
Halaman List Siswa
Untuk File Kodingan yang berubah antara lain:
index.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if(isset($_GET['status'])): ?> | |
<p> | |
<?php | |
if($_GET['status'] == 'sukses'){ | |
echo "Pendaftaran siswa baru berhasil!"; | |
} | |
else if ($_GET['status'] == 'suksesedit'){ | |
echo "Perubahan siswa berhasil!"; | |
}else { | |
echo "Pendaftaran gagal!"; | |
} | |
?> | |
</p> |
list-siswa.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table class="table table-striped" border="1"> | |
<thead> | |
<tr> | |
<th>No</th> | |
<th>Foto</th> | |
<th>NIS</th> | |
<th>Nama</th> | |
<th>Alamat</th> | |
<th>Jenis Kelamin</th> | |
<th>Agama</th> | |
<th>Sekolah Asal</th> | |
<th>Tindakan</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
$sql = "SELECT * FROM calon_siswa"; | |
$query = mysqli_query($db, $sql); | |
while($siswa = mysqli_fetch_array($query)){ | |
echo "<tr>"; | |
echo "<td>".$siswa['id']."</td>"; | |
echo "<td><img src='".$siswa['foto']."' width='100' height='100'></td>"; | |
echo "<td>".$siswa['nis']."</td>"; | |
echo "<td>".$siswa['nama']."</td>"; | |
echo "<td>".$siswa['alamat']."</td>"; | |
echo "<td>".$siswa['jenis_kelamin']."</td>"; | |
echo "<td>".$siswa['agama']."</td>"; | |
echo "<td>".$siswa['sekolah_asal']."</td>"; | |
echo "<td>"; | |
echo "<a href='form-edit.php?id=".$siswa['id']."'>Edit</a> | "; | |
echo "<a href='hapus.php?id=".$siswa['id']."'>Hapus</a>"; | |
echo "</td>"; | |
echo "</tr>"; | |
} | |
?> |
form-daftar.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p> | |
<label for="nis">NIS: </label> | |
<input type="text" name="nis" placeholder="Nomor Induk Siswa"/> | |
</p> | |
<p> | |
<label for="foto">Foto: </label> | |
<input type="file" name="foto" placeholder="Foto" id="foto"/> | |
</p> |
proses-pendaftaran.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include("config.php"); | |
// cek apakah tombol daftar sudah diklik atau blum? | |
if(isset($_POST['daftar'])){ | |
// ambil data dari formulir | |
$nis = $_POST['nis']; | |
$nama = $_POST['nama']; | |
$alamat = $_POST['alamat']; | |
$jk = $_POST['jenis_kelamin']; | |
$agama = $_POST['agama']; | |
$sekolah = $_POST['sekolah_asal']; | |
$foto = $_FILES['foto']['name']; | |
$tmp = $_FILES['foto']['tmp_name']; | |
// Rename nama fotonya dengan menambahkan tanggal dan jam upload | |
$fotobaru = date('dmYHis').$foto; | |
// Set path folder tempat menyimpan fotonya | |
$path = "images/".$fotobaru; | |
if(move_uploaded_file($tmp, $path)){ // Cek apakah gambar berhasil diupload atau tidak | |
// buat query Proses simpan ke Database | |
$sql = "INSERT INTO calon_siswa (nis, nama, alamat, jenis_kelamin, agama, sekolah_asal, foto) | |
VALUE ('$nis', '$nama', '$alamat', '$jk', '$agama', '$sekolah', '$path')"; | |
$query = mysqli_query($db, $sql); | |
// apakah query simpan berhasil? | |
if( $query ) { | |
// kalau berhasil alihkan ke halaman index.php dengan status=sukses | |
header('Location: index.php?status=sukses'); | |
} else { | |
// kalau gagal alihkan ke halaman indek.php dengan status=gagal | |
header('Location: index.php?status=gagal'); | |
} | |
} | |
} else { | |
die("Akses dilarang..."); | |
} | |
?> |
form-edit.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p> | |
<label for="nis">NIS: </label> | |
<input type="text" name="nis" placeholder="Nomor Induk Siswa" value="<?php echo $siswa['nis'] ?>" /> | |
</p> | |
<p> | |
<label for="foto">Foto: </label> | |
<input type="file" name="foto" placeholder="Foto" id="foto"/> | |
</p> |
proses-edit.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include("config.php"); | |
// cek apakah tombol simpan sudah diklik atau blum? | |
if(isset($_POST['simpan'])){ | |
// ambil data dari formulir | |
$id = $_POST['id']; | |
$nis = $_POST['nis']; | |
$nama = $_POST['nama']; | |
$alamat = $_POST['alamat']; | |
$jk = $_POST['jenis_kelamin']; | |
$agama = $_POST['agama']; | |
$sekolah = $_POST['sekolah_asal']; | |
$foto = $_FILES['foto']['name']; | |
$tmp = $_FILES['foto']['tmp_name']; | |
// Cek apakah user ingin mengubah fotonya atau tidak | |
if(empty($foto)){ // Jika user tidak memilih file foto pada form | |
// Lakukan proses update tanpa mengubah fotonya | |
$sql = "UPDATE calon_siswa SET nis='$nis', nama='$nama', alamat='$alamat', jenis_kelamin='$jk', agama='$agama', sekolah_asal='$sekolah' WHERE id=$id"; | |
$query = mysqli_query($db, $sql); | |
// apakah query update berhasil? | |
if( $query ) { | |
// kalau berhasil alihkan ke halaman list-siswa.php | |
header('Location: list-siswa.php'); | |
} else { | |
// kalau gagal tampilkan pesan | |
die("Gagal menyimpan perubahan..."); | |
} | |
} else{ // Jika user memilih foto / mengisi input file foto pada form | |
// Lakukan proses update termasuk mengganti foto sebelumnya | |
// Rename nama fotonya dengan menambahkan tanggal dan jam upload | |
$fotobaru = date('dmYHis').$foto; | |
// Set path folder tempat menyimpan fotonya | |
$path = "images/".$fotobaru; | |
// buat query update | |
if(move_uploaded_file($tmp, $path)){ // Cek apakah gambar berhasil diupload atau tidak | |
$sql = "SELECT foto FROM siswa WHERE id=$id"; | |
$query = mysqli_query($db, $sql); | |
$data = mysqli_fetch_array($query); | |
if(is_file("images/".$data['foto'])) // Jika foto ada | |
unlink("images/".$data['foto']); // Hapus file foto sebelumnya yang ada di folder images | |
// buat query | |
$sql = "UPDATE calon_siswa SET nis='$nis', foto='$path', nama='$nama', alamat='$alamat', jenis_kelamin='$jk', agama='$agama', sekolah_asal='$sekolah' WHERE id=$id"; | |
$query = mysqli_query($db, $sql); | |
// apakah query simpan berhasil? | |
if( $query ) { | |
// kalau berhasil alihkan ke halaman index.php dengan status=sukses | |
header('Location: index.php?status=suksesedit'); | |
} else { | |
// kalau gagal alihkan ke halaman indek.php dengan status=gagal | |
header('Location: index.php?status=gagal'); | |
} | |
} | |
} | |
}else { | |
die("Akses dilarang..."); | |
} | |
?> |
Komentar
Posting Komentar