Assalamualaikum Wr Wb,... Sobat Codingku PHP-MySQL, alhamdulillah artikel Contoh Aplikasi CRUD (Create Read Update Delete) Sederhana Data Mahasiswa ini dapat saya publikasikan, sebelumnya terkendala karena masalah kesibukan (sok sibuk) loh kok sok sibuk. Bagi sobat yang belum tahu pengertian CRUD itu sendiri bisa lihat artikel ini.
CRUD Sederhana Data Mahsiswa |
Adapun langkah-langkahnya sebagai berikut:
- Buat Database Kelasku
CREATE DATABASE `kelasku`;
- Buat Tabel Mahasiswa
CREATE TABLE `mhs` ( `nim` char(7) NOT NULL, `nama` varchar(20) NOT NULL, `kode_prodi` int(2) NOT NULL, PRIMARY KEY (`nim`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
- Buat Tabel Prodi
CREATE TABLE `prodi` ( `kode` char(2) NOT NULL, `nama_prodi` varchar(20) NOT NULL, PRIMARY KEY (`kode`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
- Buat folder dalam direktori C:\xampp\htdocs usahakan nama folder yang mudah diingat dan tidak terlalu panjang. Buka folder yang tadi anda buat kemudian buat folder lagi dengan nama images untuk penempatan gambar hasil upload aplikasi. Didalam folder yang pertama anda buat, buat file koneksi.php dengan menggunakan editor Notepad ++ atau program editor yang anda install.
<?php $host = "localhost"; $user = "root"; $pass = ""; $dtbs = "kelasku"; $koneksi = mysql_connect ($host,$user,$pass);//perintah login if (!$koneksi) echo "Gagal Koneksi :".mysql_error(); $pilih = mysql_select_db ($dtbs,$koneksi);//pemilihan database if (!$pilih) echo "Query Gagal : ".mysql_error(); ?>
$koneksi jika berhasil terkoneksi dengan database maka akan memilih daatabase $pilih, jika gagal memilih maka akan muncul text "Query Gagal", dan jika gagal terkoneksi ke database maka akan muncul text "Gagal Koneksi.
- Buat file tampil_data.php masih dalam folder yang sama, berfungsi sebagai halaman awal untuk menampilkan data mahsiswa sekaligus untuk melakukan semua operasi CRUD.
<?php include("koneksi.php"); $query = "Select nim, nama,nama_prodi from mhs, prodi where mhs.kode_prodi= prodi.kode"; $hasil = mysql_query ($query);// melakukan query echo "<h3>DATA MAHASISWA</h3>"; echo "<a href=form_input_mhs.html>TAMBAH DATA</a>"; echo "</br>"; echo "<table width=auto border=1 align=left>"; echo "<tr>"; echo "<th>NIM</th><th>NAMA</th><th>PROGRAM STUDI</th><th colspan=2>AKSI</th>"; echo "</tr>"; while ($kolom = mysql_fetch_row ($hasil))// menampilkan query { echo "<tr>"; echo "<td>".$kolom[0]."</td>"; echo "<td>".$kolom[1]."</td>"; echo "<td>".$kolom[2]."</td>"; echo "<td><a href=form_ubah_mhs.php?nim=".$kolom[0].">UBAH</a></td>"; echo "<td><a href=hapus_mhs.php?nim=".$kolom[0]." onClick=\"return confirm('Yakin ingin menghapus data ini ?')\">HAPUS</a></td>"; echo "</tr>"; } echo "</table>"; ?>
- Buat file form_input_mhs.php masih dalam folder yang sama, file form_input_mhs.php ini berfungsi untuk menginput data mahasiwa dari tabel mhs.
<?php include("koneksi.php");?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Form INPUT</title> </head> <body> <form name="form1" method="post" action="#"> <table width="218" border="0" align="center"> <tr> <td width="48">NIM</td> <td width="3">:</td> <td colspan="2"> <input type="text" name="nim" /> </td> </tr> <tr> <td>NAMA</td> <td>:</td> <td colspan="2"> <input type="text" name="nama" /> </td> </tr> <tr> <td>PRODI</td> <td>:</td> <td colspan="2"> <select name="prodi"> <option value=01>Teknik Informatika</option> <option value=02>Akuntansi</option> <option value=03>Administrasi Bisnis</option> </select> </td> </tr> <tr> <td> </td> <td> </td> <td width="86"> <input name="SIMPAN" type="submit" value="SIMPAN" /> </td> <td width="53"> <input name="Reset" type="reset" value="BATAL" /> </td> </tr> </table> </form> <?php if(isset($_POST['SIMPAN'])){ $nim = $_POST["nim"]; $nama = $_POST["nama"]; $prodi =$_POST["prodi"]; $query = "INSERT INTO mhs value ('$nim','$nama','$prodi')"; $hasil = mysql_query ($query);// melakukan query if ($hasil) { header ("location:tampil_data.php"); } }else{ unset($_POST['SIMPAN']); } ?> <?php $query = "Select * from mhs"; $hasil = mysql_query ($query);// melakukan query echo "<table width=auto border=1 align=center>"; echo "<tr>"; echo "<th>NIM</th><th>Nama</th><th>kode prodi</th>"; echo "</tr>"; while ($kolom = mysql_fetch_row ($hasil))// menampilkan query { echo "<tr>"; echo "<td>".$kolom[0]."</td>"; echo "<td>".$kolom[1]."</td>"; echo "<td>".$kolom[2]."</td>"; echo "</tr>"; } echo "</table>"; ?> </body> </html>
Perintah if(isset($_POST['SIMPAN'])) berfungsi meneima variabel dari buttom name="SIMPAN". Kueri untuk menambah data dalam tabel mahasiswa INSERT INTO mhs value ('$nim','$nama','$prodi'), jika berhasil disimpan maka halaman akan dialihkan ke tampil_data.php, dan jika gagal maka funsi unset akan menghapus variabel dari bottom "SIMPAN".
Aw, thіs was an incredibly nice ρost. Taking the time and actual effort to cгeate a good article… but what can I say… I pսt things off a lot and never managе
ReplyDeleteto get nearly anything done.
check my site : How To Lock Files The Marine Way
Hi! Thiѕ is my first visit to your blog! We are a c᧐llection of volunteers and
ReplyDeletestarting a new initiɑtive in a community in the same niche.
Your blog prοᴠided us valuaЬle information to ѡork on. You have ɗone a marvellous job!
Visit Website : How To Lock Files In 10 Mіnutes And Տtill Look Your Best
If you ѡiѕh for to obtain a great deal from this p᧐st then you have to apply ѕuch methօds
ReplyDeleteto your won web site.
linkеd here : Top Tips Of Encryption Softwarе & 3 Ways You
Ꮯan How To Encrypt A Password Fοr Ϝree Withⲟut Investіng Too Much Of Your Time
Oh my goodness! Amazing article dude! Thanks, However I am going through
ReplyDeleteproblems with your RSS. I don't understand why I am unable to join it.
Is there anybody getting similar RSS issues? Anyone who knows the answer can you kindly respond?
Thanks!!
Thanks for sharing your thoughts about damp.
ReplyDeleteRegards
Excellent article! We will be linking to this particularly great post on our
ReplyDeletewebsite. Keep up the great writing.
Hi there i am kavin, its my first occasion to commenting anywhere,
ReplyDeletewhen i read this piece of writing i thought i could
also create comment due to this brilliant piece
of writing.
Undeniably believe that which you said. Your favorite justification appeared to be at the web the easiest thing to have in mind of.
ReplyDeleteI say to you, I definitely get annoyed whilst people think
about worries that they just do not recognize about.
You managed to hit the nail upon the top as well as defined out the entire thing with no need side effect ,
other folks could take a signal. Will probably be back
to get more. Thank you
It's hard to find well-informed people about this topic, however, you sound like you know what you're talking about!
ReplyDeleteThanks