PHP dan MySQL (Studikasus: Login) Jumadi, S.T. Blog: jumadi.blog.ugm.ac.id Email: mcs.jumadi@mail.ugm.ac.id
AppServ:Apache+PHP+Mysql
http://localhost/phpmyadmin/
Membuat Database dan Tabel CREATE DATABASE pengunjung; CREATE TABLE pengunjung.bukutamu (nama varchar(50),email varchar(50),komentar varchar(160)); INSERT INTO pengunjung.bukutamu VALUES ('akas', 'akas@yahoo.com', 'Bandung mesti sukses'); INSERT INTO pengunjung.bukutamu VALUES ('ajum', 'ajum@gmail.com', 'Bandungku Bandungmu'); INSERT INTO pengunjung.bukutamu VALUES ('keukeu', 'kekeu@gmail.com', 'Bandung berbunga'); INSERT INTO pengunjung.bukutamu VALUES ('rossa', 'rossa@gmail.com', 'Bandung is the best');
Query pada MySQL Server
AppServ: Document Root
Form Input Data nama file: input.php <html> <body> <form name="fdata" action="http://localhost/folder/simpan.php" method="post"> <table border=“0"> <tr><td>Nama</td><td><input type="text" name="nama"></td></tr> <tr><td>E-mail</td><td><input type="text" name="email"></td></tr> <tr><td>Komentar</td><td><input type="text" name="komentar"></td></tr> <tr><td></td><td><input type="submit" name="btkirim" value="Kirim"></td></tr> </table> </form> </body> </html>
Tampilan Form Input Data file: input.php
Menyimpan Database nama file: simpan.php <html> <body> <? $conn=mysql_connect("localhost","root","123"); if ($conn){ mysql_select_db("pengunjung"); echo "Nama=$nama<br>"; echo "Email=$email<br>"; echo "Komentar=$komentar<br>"; $perintah="insert into bukutamu (nama,email,komentar) values ('$nama','$email','$komentar')"; $hasil=mysql_query($perintah,$conn); if ($hasil){ echo "<br><br>Data telah tersimpan"; } mysql_close($conn); }else{ echo "Gagal Koneksi ke server"; ?> <br><br> <a href="http://localhost/FOLDER/input.php">Input Data</a> <br> <a href="http://localhost/FOLDER/tampil.php">Tampil Data</a> </body> </html>
Menyimpan Database file: simpan.php
Menampilkan isi database nama file: tampil.php <html> <body> <? $conn=mysql_connect("localhost","root","123"); if ($conn){ mysql_select_db("pengunjung"); $perintah="select * from bukutamu"; $hasil=mysql_query($perintah,$conn); $baris=mysql_fetch_row($hasil); if ($baris){ echo "<table border='1'><tr><td>NAMA</td><td>E-MAIL</td><td>KOMENTAR</td></tr>"; do { list($nama,$email,$komentar)=$baris; echo "<tr><td>$nama</td>"; echo "<td>$email</td>"; echo "<td>$komentar</td></tr>"; }while ($baris=mysql_fetch_row($hasil)); echo "</table>"; }else{ echo "Data tidak ada"; } mysql_close($conn); echo "Gagal Koneksi ke server"; ?> <br><br> <a href="http://localhost/FOLDER/input.php">Input Data</a> </body> </html>
Hasil penampilan isi database file: tampil.php
Form pencarian Data nama file: formcari.php <html> <body> <form name="fcari" method="post" action="http://localhost/folder/cari.php"> <table border="0"> <tr><td>Masukan nama</td><td><input type="text" name="kunci"></td></tr> <tr><td></td><td><input type="submit" value="Cari"></td></tr> </table> </form> </body> </html>
Script pencarian Data nama file: cari.php <? $conn=mysql_connect("localhost","root","123"); if ($conn){ mysql_select_db("pengunjung"); $perintah="select * from bukutamu where nama LIKE '%$kunci%'"; $hasil=mysql_query($perintah,$conn); $baris=mysql_fetch_row($hasil); if ($baris){ echo "<table border='1'><tr><td>NAMA</td><td>E-MAIL</td><td>KOMENTAR</td></tr>"; do { list($nama,$email,$komentar)=$baris; echo "<tr><td>$nama</td>"; echo "<td>$email</td>"; echo "<td>$komentar</td></tr>"; }while ($baris=mysql_fetch_row($hasil)); echo "</table>"; }else{ echo "Data tidak ada"; } mysql_close($conn); echo "Gagal Koneksi ke server"; ?> <br><br> <a href="http://localhost/FOLDER/formcari.php">Cari Data</a> </body> </html>
Database pengguna
Form pencarian nama file: login.php <html> <body> <form name="fdata" action="http://localhost/folder/cek.php" method="post"> <table border="0"> <tr><td>User name</td><td><input type="text" name="pengguna"></td></tr> <tr><td>Password</td><td><input type=“password" name="sandi"></td></tr> <tr><td></td><td><input type="submit" name="btlogin" value="Login"></td></tr> </table> </form> </body> </html>
Script cek login nama file: cek.php <html> <body> <? $conn=mysql_connect("localhost","root","123"); if ($conn){ mysql_select_db("pengguna"); $perintah="select * from login where username='$pengguna'"; $hasil=mysql_query($perintah,$conn); $baris=mysql_fetch_row($hasil); if ($baris){ $cek=false; do { list($username,$password,$level)=$baris; if($password==$sandi){ $cek=true; $otoritas=$level; } }while ($baris=mysql_fetch_row($hasil)); if($cek){ echo "<h1>Selamat Datang</h1>"; switch ($otoritas){ case 1: echo "<br>Otoritas anda sebagai Administrator"; break; case 2: echo "<br>Otoritas anda sebagai Tamu"; break; } }else{ echo "Password anda salah!!!"; } }else{ echo "Username tidak terdaftar....";} mysql_close($conn); }else{ echo "Gagal Koneksi ke server"; } ?> <br><br> <a href="http://localhost/FOLDER/login.php">Back</a> </body> </html>
Hasil login
Form Input Login nama file : inputlogin.php <html> <body> <form name="fdata" action="http://localhost/folder/savelogin.php" method="post"> <table border=“0"> <tr><td>Username</td><td><input type="text" name="pengguna"></td></tr> <tr><td>Password</td><td><input type="password" name="sandi"></td></tr> <tr><td>Otoritas</td> <td><select name="level"> <option value="1">Administrator <option value="2">Operator </select> </td></tr> <tr><td></td><td><input type="submit" name="btSimpan" value="Simpan"></td></tr> </table> </form> </body> </html>
Script simpan Login nama file : savelogin.php <html> <body> <? $conn=mysql_connect("localhost","root","123"); if ($conn){ mysql_select_db("pengguna"); echo "Username=$pengguna<br>"; echo "Password=$sandi<br>"; echo "Otoritas=$level<br><br>"; $perintah="insert into login (username,password,level) values ('$pengguna','$sandi','$level')"; $hasil=mysql_query($perintah,$conn); if ($hasil){ echo "<br>Data telah tersimpan"; } mysql_close($conn); }else{ echo "Gagal Koneksi ke server"; ?> <br><br> <a href="http://localhost/FOLDER/inputlogin.php">Input Data login</a> <br> <a href="http://localhost/FOLDER/showlogin.php">Tampil Data login</a> </body> </html>
Tampilan savelogin.php
Script tampil Login nama file : showlogin.php <html> <body> <? $conn=mysql_connect("localhost","root","123"); if ($conn){ mysql_select_db("pengguna"); $perintah="select * from login"; $hasil=mysql_query($perintah,$conn); $baris=mysql_fetch_row($hasil); if ($baris){ echo "<table border='1'><tr><td>Usernamee</td><td>Password</td><td>Otoritas</td></tr>"; do { list($username,$password,$level)=$baris; if ($level==1){ $otoritas="Admnistrator"; }else{ $otoritas="Operator"; } echo "<tr><td>$username</td>"; echo "<td>$password</td>"; echo "<td>$otoritas</td></tr>"; }while ($baris=mysql_fetch_row($hasil)); echo "</table>"; echo "Data tidak ada"; mysql_close($conn); echo "Gagal Koneksi ke server"; ?> <br><br> <a href="http://localhost/FOLDER/inputlogin.php">Input Data</a> </body> </html>
Tampilan showlogin.php
Form ganti password nama file : newpassword.php <html> <body> <form name="fdata" action="http://localhost/folder/ubahpassword.php" method="post"> <table border=“0"> <tr><td>Username</td><td><input type="text" name="pengguna"></td></tr> <tr><td>Old Password</td><td><input type="password" name="sandilama"></td></tr> <tr><td>New Password</td><td><input type="password" name="sandibaru"></td></tr> <tr><td></td><td><input type="submit" name="btUbah" value="Ubah"></td></tr> </table> </form> </body> </html>
Script mengubah password namafile: ubahpassword.php <html> <body> <? $conn=mysql_connect("localhost","root","123"); if ($conn){ mysql_select_db("pengguna"); $perintah="select * from login where username='$pengguna'"; $hasil=mysql_query($perintah,$conn); $baris=mysql_fetch_row($hasil); if ($baris){ $cek=false; do { list($username,$password,$level)=$baris; if($password==$sandilama){ $cek=true; $otoritas=$level; } }while ($baris=mysql_fetch_row($hasil)); if($cek){ echo "Username=$pengguna<br>"; echo "Password=$sandibaru<br>"; echo "Otoritas=$level<br><br>"; $perintah="update login set password='$sandibaru',level='$level' where username='$pengguna'"; if ($hasil){ echo "<br>Perubahan password telah tersimpan"; }else{ echo "Password anda salah!!!"; } }else{ echo "Username tidak terdaftar....";} mysql_close($conn); }else{ echo "Gagal Koneksi ke server"; } ?> <br><br> <a href="http://localhost/FOLDER/newpassword.php">Back</a> </body> </html>
Form Hapus data nama file: formhapus.php <html> <body> <form name="fcari" method="post" action="http://localhost/folder/hapuslogin.php"> <table border="0"> <tr><td>Masukan username</td><td><input type="text" name="kunci"></td></tr> <tr><td></td><td><input type="submit" value="Hapus"></td></tr> </table> </form> </body> </html>
Script Hapus data nama file: hapuslogin.php <html> <body> <? $conn=mysql_connect("localhost","root","123"); if ($conn){ mysql_select_db("pengguna"); $perintah="delete from login where username='$kunci'"; $hasil=mysql_query($perintah,$conn); if ($hasil){ echo "<br>Data telah terhapus"; }else{ echo "<br>Data tidak ditemukan"; } mysql_close($conn); echo "Gagal Koneksi ke server"; ?> <br><br> <a href="http://localhost/FOLDER/formhapus.php">Hapus Data login</a> <br> <a href="http://localhost/FOLDER/showlogin.php">Tampil Data login</a> </body> </html>
Index.php <html> <frameset rows="20%,80%"> <frame name="atas" src="http://localhost/folder/judul.php"> <frameset cols="20%,80%"> <frame name="kiri" src="http://localhost/folder/menu.php"> <frame name="kanan" src="http://localhost/folder/isi.php"> </frameset> </html>
Menu.php <html> <body> <h2>Data Pengunjung</h2> <a href="http://localhost/FOLDER/input.php" target="kanan">Input Buku tamu</a><br> <a href="http://localhost/FOLDER/tampil.php" target="kanan">Tampil buku tamu</a><br> <br> <h2>Data Pengguna</h2> <a href="http://localhost/FOLDER/inputlogin.php" target="kanan">Input data pengguna</a><br> <a href="http://localhost/FOLDER/showlogin.php" target="kanan">Tampil data login</a><br> <a href="http://localhost/FOLDER/newpassword.php" target="kanan">Ganti Password</a><br> <a href="http://localhost/FOLDER/formhapus.php" target="kanan">Hapus data login</a><br> <a href="http://localhost/FOLDER/login.php" target="kanan">Login</a><br> </body> </html>
Judul.php <html> <body> <H1 align="center">Dasar-dasar hubungan HTML, PHP dan MySQL Server</H1> <p align="center">Belajar bersama dengan orang-orang inforamatika</p> </body> </html>
Isi.php <html> <body> <H3 align="center">Secara tersirat apa yang perlu diketahui...</H1> <p align="center">Setelah mengenal tag-tag HTML, tuntutan berikutnya adalah bagaimana HTML dapat mentransfer data ke database MySql menggunakan script PHP. Dalam memperlajari script PHP dituntut mendaptkan porsi yang lebih banyak daripada mempelajari HTML dan MySql itu sendiri. </p> </body> </html>
Session & Cookeis [akhir pertemuan]
Menampilkan data dg huruf dpn <html> <body> <? $conn=mysql_connect("localhost","root","123"); if ($conn){ mysql_select_db("pengunjung"); $perintah="select * from bukutamu where nama like '$kunci%'"; $hasil=mysql_query($perintah,$conn); $baris=mysql_fetch_row($hasil); if ($baris){ echo "<table border='1'><tr><td>NAMA</td><td>E-MAIL</td><td>KOMENTAR</td></tr>"; do { list($nama,$email,$komentar)=$baris; echo "<tr><td>$nama</td>"; echo "<td>$email</td>"; echo "<td>$komentar</td></tr>"; }while ($baris=mysql_fetch_row($hasil)); echo "</table>"; }else{ echo "Data tidak ada"; } mysql_close($conn); echo "Gagal Koneksi ke server"; echo "<br>"; for ($i=65; $i<91; $i++){ $huruf=chr($i); echo "| <a href='http://localhost/folder/kasus1.php?kunci=$huruf'>$huruf</a>"; echo " |"; ?> <br><br> <a href="http://localhost/FOLDER/input.php">Input Data</a> </body> </html>
Kasus 2: Hapus & Edit Login <html> <body> <? $conn=mysql_connect("localhost","root","123"); if ($conn){ mysql_select_db("pengguna"); $perintah="select * from login"; $hasil=mysql_query($perintah,$conn); $baris=mysql_fetch_row($hasil); if ($baris){ ?> <table border='1'><tr><td>Usernamee</td><td>Password</td><td>Otoritas</td><td colspan="2"><div align="center">Perintah</div></td></tr> do { list($username,$password,$level)=$baris; if ($level==1){ $otoritas="Admnistrator"; }else{ $otoritas="Operator"; } <tr> <td><? echo $username ?></td> <td><? echo $password?></td> <td><? echo $otoritas?></td> <td><a href="http://localhost/folder/editk2.php?pengguna=<? echo $username ?>">Edit</a></td> <td><a href="http://localhost/folder/hapuslogin.php?kunci=<? echo $username ?>">Hapus</a></td> </tr> }while ($baris=mysql_fetch_row($hasil)); ?></table><? echo "Data tidak ada"; mysql_close($conn); echo "Gagal Koneksi ke server"; <br><br> <a href="http://localhost/FOLDER/inputlogin.php">Input Data</a> </body> </html>
Editk2.php <html> <body> <form name="fdata" action="http://localhost/folder/ubahpassword.php" method="post"> <table border=“0"> <tr><td>Username</td> <td><input type="text" value="<? echo $pengguna ?>" name="pengguna"></td> </tr> <tr><td>Old Password</td><td><input type="password" name="sandilama"></td></tr> <tr><td>New Password</td><td><input type="password" name="sandibaru"></td></tr> <tr><td></td><td><input type="submit" name="btUbah" value="Ubah"></td></tr> </table> </form> </body> </html>