Pertama kita buat dulu Database dengan nama “dbanggota1”. Dengan struktur tabel seperti dibawah ini :
Struktur Tabel Anggota create table anggota( nomor int(6)primary key, nama varchar(40)not null, email varchar(50)not null, alamat varchar(80)not null, kota varchar(50)not null ); insert into anggota values(1,'Ema Mitsikuri','mitsikuri@gmail.com','Jl.ketapang','Jambi'); insert into anggota values(2,'Tono Subroto','tono@yahoo.com','Jl.Dr.Soetomo','Padang'); insert into anggota values(3,'Budi Krniawan','budi@plasa.com','Jl.Anggrek','Pekanbaru'); insert into anggota values(4,'Ani Mariani','ani@yahoo.com','Jl.Mawar','Jambi'); insert into anggota values(5,'Dian Nur Cahaya','dian@yahoo.co.id','Jl.Beringin','Jakarta');
Gambar 1 - Form Input_anggota.
Kode program - input_anggota.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>Input Anggota</title> <style type="text/css"> <!-- .style1 {color: #FFFFFF} --> </style> </head>
<body> <form id="form1" name="form1" method="post" action="simpan.php"> <table width="406" border="3" align="center"> <tr bgcolor="#009999"> <td colspan="2"><div align="center"><span class="style1">INPUT ANGGOTA </span></div></td> </tr> <tr bgcolor="#FFCCCC"> <td width="85">Nomor</td> <td width="300"><input name="nomor" type="text" id="nomor" size="6" /></td> </tr> <tr bgcolor="#009999"> <td>Nama</td> <td><input name="nama" type="text" id="nama" size="50" /></td> </tr> <tr bgcolor="#FFCCCC"> <td>Email</td> <td><input name="email" type="text" id="email" size="50" maxlength="80" /></td> </tr> <tr bgcolor="#009999"> <td align="left" valign="top">Alamat</td> <td><textarea name="alamat" cols="47" rows="5" id="alamat"></textarea></td> </tr> <tr bgcolor="#FFCCCC"> <td>Kota</td> <td><input name="kota" type="text" id="kota" size="50" /></td> </tr> <tr bgcolor="#FFCCCC"> <td colspan="2"> <div align="right"> <a href="ListAnggota.php">List Anggota </a> <input type="submit" name="Submit" value="SAVE" /> <input type="reset" name="Submit2" value="CLEAR" /> </div></td> </tr> </table> </form> </body> </html>
Kode Program - Simpan.php
<html> <head> <title>Simpan data Anggota</title> </head> <body bgcolor="#99CCFF"> <font color="#000066" face=verdana> <h3 align=left> <?php require("ConnectDb.php"); $link=open_connection(); $tablename="anggota"; $sqlstr="INSERT INTO $tablename VALUES('$nomor','$nama','$email','$alamat','$kota')"; if (!mysql_query ($sqlstr)) { echo("Invalid Query!<br>Please register again...<br>"); exit; } mysql_close($link); echo("Terima Kasih Telah Mengisi Data Anggota...<p>"); ?> </h3> </font> <font color="#000066" face=verdana size=2> |<a href="InputAnggota.php"> Back</a> | <a href="ListAnggota.php">Lihat Data</a> | </font> </body> </html>
Gambar 2 Simpan_data_anggota.
Kode Program ConnectDb.php <?php function open_connection() { $host="localhost"; $username="root"; $password=""; $databasename="dbanggota1"; $link=mysql_connect($host,$username,$password) or die ("Database tidak dapat dihubungkan!"); mysql_select_db($databasename,$link); return $link; } ?>
Gambar 3 – ListAnggota.
<html> <head> <title>Movie database</title> <style type="text/css"> TD{color:#353535;font-family:verdana} TH{color:#FFFFFF;font-family:verdana;background-color:#336699} .style2 {color: #400080} .style3 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 24px; } </style> </head> <body> <blockquote> <h2 align="center" class="style2">DAFTAR NAMA ANGGOTA </h2> </blockquote> <table border="1" width="774" cellspacing="1" cellpadding="3" bgcolor="" align="center"> <tr> <td> Nomor </td> <td>Nama Anggota </td> <td>Email</td> <td> Alamat </td> <td>Kota</td> </tr> <tr> <td bgcolor="#FFFFFF" colspan="9" align="center"> Input Anggota Baru <a href="inputanggota.php"> [ADD]</a></td> </tr> <?php require("ConnectDb.php"); $link=open_connection(); $tablename="anggota"; $sqlstr="select * from $tablename"; $result = mysql_query ($sqlstr) or die ("Kesalahan pada perintah SQL!"); while ($row = mysql_fetch_array($result)) { ?> <tr> <td bgcolor="#FFFFFF" width="15%"> <?php echo $row['nomor']; ?> </td> <td bgcolor="#FFFFFF" width="41%"> <?php echo $row['nama']; ?> </td> <td bgcolor="#FFFFFF" width="19%"> <?php echo $row['email']; ?> </td> <td bgcolor="#FFFFFF" width="25%"> <?php echo $row['alamat']; ?></td> <td bgcolor="#FFFFFF" width="25%"><?php echo $row['kota']; ?></td> </tr> <?php } ?> </table> </body> </html>
Gambar 4 - Delete Anggota.
<html> <head> <title>Movie database</title> <style type="text/css"> TD{color:#353535;font-family:verdana} TH{color:#FFFFFF;font-family:verdana;background-color:#336699} .style2 {color: #400080} .style3 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 24px; } </style> </head> <body> <blockquote> <h2 align="center" class="style2">DAFTAR NAMA ANGGOTA </h2> <p align="left" class="style2 style3"> <a href="index.html"></a></p> </blockquote> <table border="1" width="774" cellspacing="1" cellpadding="3" bgcolor="" align="center"> <tr> <td> Nomor </td> <td>Nama Anggota </td> <td>Email</td> <td> Alamat </td> <td>Kota</td> <td>Action</td> </tr> <tr> <td bgcolor="#FFFFFF" colspan="10" align="center"> Input Anggota Baru <a href="inputanggota.php"> [ADD]</a></td> </tr> <?php require("ConnectDb.php"); $link=open_connection(); $tablename="anggota"; $sqlstr="select *from $tablename"; $result = mysql_query ($sqlstr) or die ("Kesalahan pada perintah SQL!"); while ($row = mysql_fetch_array($result)) { ?> <tr> <td bgcolor="#FFFFFF" width="15%"> <?php echo $row['nomor']; ?> </td> <td bgcolor="#FFFFFF" width="41%"> <?php echo $row['nama']; ?> </td> <td bgcolor="#FFFFFF" width="19%"> <?php echo $row['email']; ?> </td> <td bgcolor="#FFFFFF" width="25%"> <?php echo $row['alamat']; ?></td> <td bgcolor="#FFFFFF" width="25%"><?php echo $row['kota']; ?></td> <td bgcolor="#FFFFFF" width="25%"><a href="delete.php?&nomor=<?php echo $row['nomor']?>">[Delete]</a></td> </tr> <?php } ?> </table> </body> </html>
Kode Program - Delete.php <?php // COMMIT ADD require("ConnectDb.php"); $link=open_connection(); $tablename="anggota"; $sql = "Delete from $tablename where nomor='$nomor' "; $result = mysql_query($sql) or die("Invalid query: " . mysql_error()); ?> <p align="center" style="color:#FF0000"> Berhasil DiDelete..! <a href="list2.php">Back to</a> </p> <?php
Gambar 5 - Proses Hapus.
Gambar 6 - Update Anggota.
Kode Program - List4.php <html> <head> <title>Movie database</title> <style type="text/css"> TD{color:#353535;font-family:verdana} TH{color:#FFFFFF;font-family:verdana;background-color:#336699} .style2 {color: #400080} .style3 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 24px; } </style> </head> <body> <blockquote> <h2 align="center" class="style2">DAFTAR NAMA ANGGOTA </h2> <p align="left" class="style2 style3"> <a href="index.html"></a></p> </blockquote> <table border="1" width="774" cellspacing="1" cellpadding="3" bgcolor="" align="center"> <tr> <td> Nomor </td> <td>Nama Anggota </td> <td>Email</td> <td> Alamat </td> <td>Kota</td> <td>Action</td> </tr> <tr> <td bgcolor="#FFFFFF" colspan="10" align="center"> Input Anggota Baru <a href="inputanggota.php"> [ADD]</a></td> </tr> <?php require("ConnectDb.php"); $link=open_connection(); $tablename="anggota"; $sqlstr="select *from $tablename"; $result = mysql_query ($sqlstr) or die ("Kesalahan pada perintah SQL!"); while ($row = mysql_fetch_array($result)) { ?> <tr> <td bgcolor="#FFFFFF" width="15%"> <?php echo $row['nomor']; ?> </td> <td bgcolor="#FFFFFF" width="41%"> <?php echo $row['nama']; ?> </td> <td bgcolor="#FFFFFF" width="19%"> <?php echo $row['email']; ?> </td> <td bgcolor="#FFFFFF" width="25%"> <?php echo $row['alamat']; ?></td> <td bgcolor="#FFFFFF" width="25%"><?php echo $row['kota']; ?></td> <td bgcolor="#FFFFFF" width="25%"><a href="delete.php?&nomor=<?php echo $row['nomor']?>">[Delete]</a> <a href="tampilupdate.php?&nomor=<?php echo $row['nomor']?>">[Edit]</a></td> </tr> <?php } ?> </table> </body> </html>
Kode Program - Update.php
<?php // COMMIT ADD require("ConnectDb.php"); $link=open_connection(); $tablename="anggota"; $sql = "update $tablename set nomor='$nomor', nama='$nama', email= '$email',alamat='$alamat', kota='$kota' where nomor='$nomor' "; $result = mysql_query($sql) or die("Invalid query: " . mysql_error()); ?> <p align="center" style="color:#FF0000"> Berhasil DiUpdate..! <a href="list4.php">Back to</a> </p>
Gambar 7 – Proses Update.
Gambar 8 - Tampilupdate.
<?php require("ConnectDb.php"); $link=open_connection(); $tablename="anggota"; $sql1 = "SELECT * FROM $tablename WHERE nomor = $nomor "; $result = mysql_query($sql1) or die(" Test Invalid query: " . mysql_error()); $row = mysql_fetch_array($result); $nomor= $row['nomor']; $nama= $row['nama']; $email = $row['email']; $alamat = $row['alamat']; $kota = $row['kota']; ?>
<html> <head> <title>Update Anggota</title> <style type="text/css"> <!-- .style1 {color: #FFFFFF} --> </style> </head> <body> <form id="form1" name="form1" method="post" action="update.php"> <table width="406" border="3" align="center"> <tr bgcolor="#009999"> <td colspan="2"><div align="center"><span class="style1">UPDATE ANGGOTA </span></div></td> </tr> <tr bgcolor="#FFCCCC"> <td width="85">Nomor</td> <td width="300"><input name="nomor" type="text" id="nomor2" value="<? echo $nomor ?> " size="6" /></td> </tr> <tr bgcolor="#009999"> <td>Nama</td> <td><input name="nama" type="text" id="nama" value="<? echo $nama ?> " size="50" /></td> </tr> <tr bgcolor="#FFCCCC"> <td>Email</td> <td><input name="email" type="text" id="email" value="<? echo $email ?> " size="50" maxlength="80" /></td> </tr> <tr bgcolor="#009999"> <td align="left" valign="top">Alamat</td> <td><textarea name="alamat" cols="47" rows="5" id="alamat"><? echo $alamat ?> </textarea></td> </tr> <tr bgcolor="#FFCCCC"> <td>Kota</td> <td><input name="kota" type="text" id="kota" value="<? echo $kota ?> " size="50" /></td> </tr> <tr bgcolor="#FFCCCC"> <td colspan="2"> <div align="right"> <a href="List4.php">List Anggota </a> <input type="submit" name="Submit" value="Update" /> <input type="reset" name="Submit2" value="Clear" /> </div></td> </tr> </table> </form> </body> </html> |
Sumber : Praktikum Pemrograman WEB, STMIK Mercusuar – 2010 | Download Article. |
Sepertinya Kenal banget INi Skript.. oh ternyata baru ingat.. Modul Kuliah ku xiciciciici...
BalasHapusnice share gan :D