Proses tampil data, input, update dan delete sering kita jumpai. Atau dalam bahasa kerennya di sebut CRUD (Creata, Read, Update, Delete) yang merupakan proses pemrograman dasar khususnya yang berkaitan dengan database. Untuk pemrograman bebasis WEB proses CRUD ini dapat dilakukan dengan kode script PHP, baik dalam proses CRUD maupun untuk mehubungkan database dengan program PHP yang kita buat.
Pada kesempatan kali ini saya akan berbagi contoh sistem CRUD Pendataan data guru dan siswa.
Terlebih dahulu buatlah database, dalam contoh ini saya menggunakan phpMyAdmin.
Nama Tabel : tb_siswa & tb_guru
1. Buatlah database dengan nama db_sekolah - Create
<?php
#MySql Login
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname ='db_sekolah';
#Koneksi ke database
$koneksi= mysql_connect($dbhost, $dbuser,$dbpass);
if (! $koneksi) {
echo " koneksi gagal, Silakan Cek hostname, username dan password MYSQL...!";
echo mysql_error();
}else
{
mysql_select_db($dbname,$koneksi)
or die ("Koneksi ke Database Gagal...!".mysql_error());
}
# Fungsi untuk membuat format rupiah pada angka (uang)
function format_angka($angka) {
$hasil = number_format($angka,0, ",",".");
return $hasil;
}
?>
2. siswa.php<div class="box-header with-border">
<h3 class="box-title" align="center">Data Daftar Siswa </h3>
</div>
<div class="box-body">
<?php
if(isset($_GET['edit'])){
$edit = mysql_query("select * from tb_siswa where nis='$_GET[edit]'")or die(mysql_error());
$r=mysql_fetch_array($edit);
echo'<form action="" method="post">
<table border="0" class="data">
<br><br>
<div class="box-body">
<div class="row">
<label class="col-sm-2 control-label">NIS</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="nis" value="'.$_GET['edit'].'" readonly="readonly">
</div>
</div>
</div> <br>
<div class="box-body">
<div class="row">
<label class="col-sm-2 control-label">Nama Siswa</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="nm_siswa" value="'.$r['nm_siswa'].'" required="required" />
</div>
</div>
</div> <br>
<div class="box-body">
<div class="row">
<label class="col-sm-2 control-label">Jenis Kelamin</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="jk" value="'.$r['jk'].'" required="required" />
</div>
</div>
</div> <br>
<div class="box-body">
<div class="row">
<label class="col-sm-2 control-label">Tempat Tanggal Lahir</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="ttl" value="'.$r['ttl'].'" required="required" />
</div>
</div>
</div> <br>
<div class="box-body">
<div class="row">
<label class="col-sm-2 control-label">Alamat</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="alamat" value="'.$r['alamat'].'" required="required" />
</div>
</div>
</div> <br>
<div class="box-body">
<div class="row">
<label class="col-sm-2 control-label">No HP</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="nohp" value="'.$r['nohp'].'" size="50" required="required" />
<br>
<input type="submit" class="btn btn-info" name="ubah" value="UPDATE" class="button" />
<a href=?page=siswa class="btn btn-danger">CANCEL</a>
</div>
</div>
</div>
</form>
</table>';
if(isset($_POST['ubah'])){
mysql_query("update tb_siswa set nm_siswa='$_POST[nm_siswa]',jk='$_POST[jk]',ttl='$_POST[ttl]',alamat='$_POST[alamat]',nohp='$_POST[nohp]' where nis='$_POST[nis]'")or die(mysql_error());
echo "<script>alert('Data Berhasil Diubah'); window.location = '?page=siswa'</script>";
}
}elseif(isset($_GET['hapus'])){
mysql_query("delete from tb_siswa where nis='$_GET[hapus]'")or die(mysql_error());
echo "<script>alert('Data Berhasil Dihapus'); window.location = '?page=siswa'</script>";
}else{
?>
<form role="form" action="" method="post">
<table border="0" class="data">
<!-- text input -->
<div class="form-group">
<label>NIS</label>
<input type="text" class="form-control" placeholder="" name="nis" required/>
</div>
<div class="form-group">
<label>Nama Siswa</label>
<input type="text" class="form-control" placeholder="" name="nm_siswa" required/>
</div>
<div class="form-group">
<label>Jenis Kelamin</label>
<select name="jk" class="form-control">
<option value="laki-laki">Laki-laki</option>
<option value="perempuan">Perempuan</option>
</select>
</div>
<div class="form-group">
<label>Tempat Tanggal Lahir</label>
<input type="text" class="form-control" placeholder="" name="ttl" required/>
</div>
<div class="form-group">
<label>Alamat</label>
<input type="text" class="form-control" placeholder="" name="alamat" required/>
</div>
<div class="form-group">
<label>No HP</label>
<input type="text" class="form-control" placeholder="" name="nohp" required/>
</div>
<br>
<div class="box-footer">
<button type="submit" name="simpan" class="btn btn-primary">Simpan</button>
</div>
<br>
</div>
</table>
</form>
<?php
if(isset($_POST['simpan'])){
mysql_query("insert into tb_siswa set nis='$_POST[nis]',nm_siswa='$_POST[nm_siswa]',jk='$_POST[jk]',ttl='$_POST[ttl]',alamat='$_POST[alamat]',nohp='$_POST[nohp]'");
echo "<script>window.alert('Data Berhasil .');window.location=('?page=siswa')</script>";
}
?>
<div class="panel panel-default">
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>No</th>
<th>NIS</th>
<th>Nama Siswa</th>
<th>Jenis Kelamin</th>
<th>Tempat Tanggal Lahir</th>
<th>Alamat</th>
<th>No HP</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
$query=mysql_query("SELECT * FROM tb_siswa");
while($row=mysql_fetch_array($query)){
echo"
<tr>
<td>$no</td>
<td>$row[nis]</td>
<td>$row[nm_siswa]</td>
<td>$row[jk]</td>
<td>$row[ttl]</td>
<td>$row[alamat]</td>
<td>$row[nohp]</td>
<td>
<a href='?page=siswa&edit=$row[nis]' class='btn btn-warning'>Edit</a>
<a href='?page=siswa&hapus=$row[nis]' class='btn btn-danger'>Hapus</a>
</td>
</tr>";
$no++;
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div><!-- /.box-body -->
3. guru.php<div class="box-header with-border">
<h3 class="box-title" align="center">Data Daftar Guru </h3>
</div>
<div class="box-body">
<?php
if(isset($_GET['edit'])){
$edit = mysql_query("select * from tb_guru where nik='$_GET[edit]'")or die(mysql_error());
$r=mysql_fetch_array($edit);
echo'<form action="" method="post">
<table border="0" class="data">
<br><br>
<div class="box-body">
<div class="row">
<label class="col-sm-2 control-label">NIK</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="nik" value="'.$_GET['edit'].'" readonly="readonly">
</div>
</div>
</div> <br>
<div class="box-body">
<div class="row">
<label class="col-sm-2 control-label">Nama Guru</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="nm_guru" value="'.$r['nm_guru'].'" required="required" />
</div>
</div>
</div> <br>
<div class="box-body">
<div class="row">
<label class="col-sm-2 control-label">Jenik Kelamin</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="jk" value="'.$r['jk'].'" required="required" />
</div>
</div>
</div> <br>
<div class="box-body">
<div class="row">
<label class="col-sm-2 control-label">Tempat Tanggal Lahir</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="ttl" value="'.$r['ttl'].'" required="required" />
</div>
</div>
</div> <br>
<div class="box-body">
<div class="row">
<label class="col-sm-2 control-label">Alamat</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="alamat" value="'.$r['alamat'].'" required="required" />
</div>
</div>
</div> <br>
<div class="box-body">
<div class="row">
<label class="col-sm-2 control-label">No HP</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="nohp" value="'.$r['nohp'].'" size="50" required="required" />
<br>
<input type="submit" class="btn btn-info" name="ubah" value="UPDATE" class="button" />
<a href=?page=guru class="btn btn-danger">CANCEL</a>
</div>
</div>
</div>
</form>
</table>';
if(isset($_POST['ubah'])){
mysql_query("update tb_guru set nm_guru='$_POST[nm_guru]',jk='$_POST[jk]',ttl='$_POST[ttl]',alamat='$_POST[alamat]',nohp='$_POST[nohp]' where nik='$_POST[nik]'")or die(mysql_error());
echo "<script>alert('Data Berhasil Diubah'); window.location = '?page=guru'</script>";
}
}elseif(isset($_GET['hapus'])){
mysql_query("delete from tb_guru where nik='$_GET[hapus]'")or die(mysql_error());
echo "<script>alert('Data Berhasil Dihapus'); window.location = '?page=guru'</script>";
}else{
?>
<form role="form" action="" method="post">
<table border="0" class="data">
<!-- text input -->
<div class="form-group">
<label>NIK</label>
<input type="text" class="form-control" placeholder="" name="nik" required/>
</div>
<div class="form-group">
<label>Nama Guru</label>
<input type="text" class="form-control" placeholder="" name="nm_guru" required/>
</div>
<div class="form-group">
<label>Jenik Kelamin</label>
<select name="jk" class="form-control">
<option value="laki-laki">Laki-laki</option>
<option value="perempuan">Perempuan</option>
</select>
</div>
<div class="form-group">
<label>Tempat Tanggal Lahir</label>
<input type="text" class="form-control" placeholder="" name="ttl" required/>
</div>
<div class="form-group">
<label>Alamat</label>
<input type="text" class="form-control" placeholder="" name="alamat" required/>
</div>
<div class="form-group">
<label>No HP</label>
<input type="text" class="form-control" placeholder="" name="nohp" required/>
</div>
<br>
<div class="box-footer">
<button type="submit" name="simpan" class="btn btn-primary">Simpan</button>
</div>
<br>
</div>
</table>
</form>
<?php
if(isset($_POST['simpan'])){
mysql_query("insert into tb_guru set nik='$_POST[nik]',nm_guru='$_POST[nm_guru]',jk='$_POST[jk]',ttl='$_POST[ttl]',alamat='$_POST[alamat]',nohp='$_POST[nohp]'");
echo "<script>window.alert('Data Berhasil .');window.location=('?page=guru')</script>";
}
?>
<div class="panel panel-default">
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>No</th>
<th>NIK</th>
<th>Nama Guru</th>
<th>Jenik Kelamin</th>
<th>Tempat Tanggal Lahir</th>
<th>Alamat</th>
<th>No HP</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
$query=mysql_query("SELECT * FROM tb_guru");
while($row=mysql_fetch_array($query)){
echo"
<tr>
<td>$no</td>
<td>$row[nik]</td>
<td>$row[nm_guru]</td>
<td>$row[jk]</td>
<td>$row[ttl]</td>
<td>$row[alamat]</td>
<td>$row[nohp]</td>
<td>
<a href='?page=guru&edit=$row[nik]' class='btn btn-warning'>Edit</a>
<a href='?page=guru&hapus=$row[nik]' class='btn btn-danger'>Hapus</a>
</td>
</tr>";
$no++;
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div><!-- /.box-body -->
4. dashboard.php<div class="box-header with-border">
<h4><b><div align="center">SMKN 1 Bengkalis </h4><b><br>
<div align="center">
<p><img src="assets/img/sokolah.png"/>
</p>
</p>
<p> </p>
<h5><div align="center"><marquee>copyright ahmadi | SMKN 1 Bengkalis | 2019</marquee></h5>
</div>
</div>
<!-- /.box-body -->
5. fasilitas.php<div class="box-header with-border">
<h3 class="box-title" align="center">Data Daftar Fasilitas </h3>
</div>
</div>
</div><!-- /.box-body -->
6. laporan.php<div class="box-header with-border">
<h3 class="box-title" align="center">Laporan </h3>
</div>
</div>
</div><!-- /.box-body -->
Disini Menu fasilitas dan laporan belum ada prosesnya, Untuk Pengembangan selanjutnya bisa di kembangkan lebih luas lagi, selamat mencoba...Source Code di atas bisa anda download di bawah ini
Jika ada pertanyaan, silahkan tinggalkan komentarnya.
by : admin
Disimpan dimana filenya bang kok error
ReplyDeleteSimpan Pada aplikasi XAMPP, folder htdocs yang berada di C:\xampp\htdocs.
DeleteErrornya itu masalah db unknown
ReplyDeleteKemungkinan kesalahan pada penamaan databasenya, coba di cek penamaan database yg di phpmyadmin sama di file php koneksinya,
Deletepenamaan databasenya wajib sama
password buat ekstrak apa min?
ReplyDeletePass:duniacermin21.blogspot.com
Deletemin datanya kok bisa keluar macam ne waktu kita isi data
ReplyDeleteYou have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near at line 1
Maaf, yaa.. Saya izin web ini menjadi Link (Referensi), khusus nya untuk saya belajar juga, yaaa.. Terima kasih..
ReplyDeleteSekian, terima kasih, dan salam..
Salam..
baik silakan semoga bermanfaat
DeletePost a Comment
Berkomentarlah yang sopan dan jangan buang waktu untuk melakukan spam. Terimakasih