web programming - db buku tamu

7
Andriansyah Doni www.freak-kutuonline.com Page 1 WEB PROGRAMMING - BUKU TAMU Sebelum membuat tampilan form buku tamu, terlebih dahulu buat database dengan nama dbBerita menggunakan MySQL (bisa melalui localhost/phpmyadmin). Kemudian buat tabel dengan nama buku_tamu, berikut struktur tabel buku_tamu. Field name Type Length/value Index Extra Id Int 5 Primary key Auto_increment Nama Varchar 30 Email Varchar 50 Pesan Text Sebelum membuat tampilan form, buat dulu file koneksi untuk menghubungkan form dengan database. Script program conn.php <?php $host="localhost"; $user="root"; $passwd=" "; $db="dbBerita"; $koneksi=mysql_connect($host,$user,$passwd)or die(mysql_error()); ?> Buat tampilan form buku tamu menggunakan php editor seperti PHPCoder, PSPad, Dreamweaver, atau yang lainnya (recommended gunakan Dreamweaver versi apa aja). Gambar 1. bukuTamu.php. Script program bukuTamu.php <body> <form action="simpanTamu.php" method="post"> <table width="500" border="0" align="center" cellpadding="0"> <tr> <td colspan="2" align="center" bgcolor="#999999"><strong>BUKU TAMU</strong><br />

Upload: doni-andriansyah

Post on 18-Jul-2015

243 views

Category:

Education


22 download

TRANSCRIPT

Page 1: Web Programming - DB Buku Tamu

Andriansyah Doni

w w w . f r e a k - k u t u o n l i n e . c o m

Page 1

WEB PROGRAMMING - BUKU TAMU Sebelum membuat tampilan form buku tamu, terlebih dahulu buat database dengan nama dbBerita menggunakan MySQL (bisa melalui localhost/phpmyadmin). Kemudian buat tabel dengan nama buku_tamu, berikut struktur tabel buku_tamu.

Field name Type Length/value Index Extra Id Int 5 Primary key Auto_increment Nama Varchar 30 Email Varchar 50 Pesan Text Sebelum membuat tampilan form, buat dulu file koneksi untuk menghubungkan form dengan database. Script program conn.php <?php $host="localhost"; $user="root"; $passwd=" "; $db="dbBerita"; $koneksi=mysql_connect($host,$user,$passwd)or die(mysql_error()); ?> Buat tampilan form buku tamu menggunakan php editor seperti PHPCoder, PSPad, Dreamweaver, atau yang lainnya (recommended gunakan Dreamweaver versi apa aja).

Gambar 1. bukuTamu.php. Script program bukuTamu.php <body> <form action="simpanTamu.php" method="post"> <table width="500" border="0" align="center" cellpadding="0"> <tr> <td colspan="2" align="center" bgcolor="#999999"><strong>BUKU TAMU</strong><br />

Page 2: Web Programming - DB Buku Tamu

Andriansyah Doni

w w w . f r e a k - k u t u o n l i n e . c o m

Page 2

<?php echo "Tanggal "; echo date("d F Y"); ?></td> </tr> <tr> <td width="100">Nama</td> <td><input name="nama" type="text" size="40" />&nbsp;</td> </tr> <tr> <td width="100">Email</td> <td><input name="email" type="text" size="40" />&nbsp;</td> </tr> <tr> <td width="100" valign="top">Pesan</td> <td><textarea name="pesan" cols="40" rows="3"></textarea>&nbsp;</td> </tr> <tr> <td colspan="2" align="right" bgcolor="#99FFFF"> <a href="lihatTamu.php">Lihat Daftar Tamu</a> <input name="submit" type="submit" value="Kirim" /><input name="reset" type="reset" value="Batal" />&nbsp;</td> </tr> </table> </form> </body> Berikutnya buat tampilan simpanTamu.php untuk menyimpan data tamu kedalam tabel buku_tamu. Kronologis jalannya program Ketika user menginput data tamu kemudian meng-klik tombol kirim, maka data yang di inputkan akan tersimpan ke dalam tabel buku_tamu. Pada address bar browser akan di tampilkan pesan “Data tamu telah tersimpan”. Tampilan form buku tamu akan kembali dikosongkan.

Gambar 2. Proses kirim data tamu.

Page 3: Web Programming - DB Buku Tamu

Andriansyah Doni

w w w . f r e a k - k u t u o n l i n e . c o m

Page 3

script program simpanTamu.php <?php $nm=$_POST['nama']; $email=$_POST['email']; $ps=$_POST['pesan']; include "conn.php"; mysql_select_db($db,$koneksi)or die(mysql_error()); $query=mysql_query("INSERT INTO buku_tamu VALUES(' ','$nm','$email','$ps')",$koneksi)or die(mysql_error()); echo "<meta http-equiv='refresh' content='0;url=bukuTamu.php?Data tamu telah tersimpan'>"; ?> Untuk melihat hasil data tamu yang tersimpan, klik link Lihat Daftar Tamu pada form buku tamu. sebelum itu, buat dulu file lihatTamu.php.

Gambar 3. lihatTamu.php. script program lihatTamu.php <body> <table width="800" border="1"> <tr bgcolor="#666666"> <td width="35"><div align="center">Id Tamu</div></td> <td width="110"><div align="center">Nama Tamu</div></td> <td width="120"><div align="center">Email</div></td> <td width="150"><div align="center">Pesan</div></td> <td width="75"><div align="center">Aksi</div></td> </tr> <?php include "conn.php"; $batas=5; $halaman=$_GET['halaman']; if(empty($halaman)){ $posisi=0; $halaman=1;

Page 4: Web Programming - DB Buku Tamu

Andriansyah Doni

w w w . f r e a k - k u t u o n l i n e . c o m

Page 4

} else { $posisi=($halaman-1)*$batas; } mysql_select_db($db,$koneksi)or die(mysql_error()); $query=mysql_query("SELECT * FROM buku_tamu LIMIT $posisi,$batas"); while($row = mysql_fetch_array($query)){ ?> <tr> <td><?php echo "$row[id]"; ?> <div align="center"></div></td> <td><?php echo "$row[nama]"; ?> <div align="center"></div></td> <td><?php echo "$row[email]"; ?> <div align="center"></div></td> <td><?php echo "$row[pesan]"; ?> <div align="center"></div></td> <td><div align="center"><a href="editTamu.php?id= <?php echo $row[id]; ?>"> Edit | </a> <a href="hapusTamu.php?id= <?php echo $row[id]; ?>"> Hapus </a></div></td> </tr> <?php } ?> </table> <?php $query2= mysql_query("select * from buku_tamu"); $num = mysql_num_rows($query2); $jmlhalaman = ceil($num/$batas); echo "<br>Halaman : "; for($i=1;$i<=$jmlhalaman;$i++){ if($i != $halaman){ echo "<a href=lihatTamu.php?halaman=$i>$i</a> | "; } else { echo "<b>$i</b>|"; } } ?> <p><a href="bukuTamu.php">Tambah Tamu</a> </p> </body>

Page 5: Web Programming - DB Buku Tamu

Andriansyah Doni

w w w . f r e a k - k u t u o n l i n e . c o m

Page 5

Selanjutnya membuat file editTamu.php yang berfungsi untuk mengedit data tamu maupun pesan.

Gambar 4. editTamu.php. Script program editTamu.php <body> <?php include "conn.php"; mysql_select_db($db,$koneksi)or die(mwysql_error()); $tampil=mysql_query("select * from buku_tamu where id='$_GET[id]'"); while ($row=mysql_fetch_array($tampil)) { ?> <form action="updateTamu.php" method="post" enctype="multipart/form-data"> <table border="1" align="center"><input name="txtId" type="hidden" value="<?php echo $row[id]; ?>"> <tr> <td>Id Tamu</td> <td><input type="text" name="txtId" disabled="disabled" value="<?php echo $row[id]; ?>"></td> </tr> <tr> <td>Nama Tamu</td> <td><input type="text" name="txtNama" size="50" value="<?php echo $row[nama]; ?>"></td> </tr> <tr> <td>Email</td> <td><input type="text" name="txtEmail" size="50" value="<?php echo $row[email]; ?>"></td> </tr> <tr> <td valign="top">Pesan</td> <td><input type="text" name="txtPesan" size="50" value="<?php echo $row[pesan]; ?>"></td> </tr> <tr>

Page 6: Web Programming - DB Buku Tamu

Andriansyah Doni

w w w . f r e a k - k u t u o n l i n e . c o m

Page 6

<td height="21" colspan="2" align="right" bgcolor="#666666"><input type="submit" value="Update"></td> </tr> </table> </form> <?php }?> </body> Simpan kembali data tamu yang telah di edit, klik tombol Update. Form daftar data tamu akan di tampilkan kembali dengan data tamu yang telah di update.

Gambar 5. updateTamu.php. Script program updateTamu.php <body> <?php $id=$_POST['txtId']; $nm=$_POST['txtNama']; $em=$_POST['txtEmail']; $ps=$_POST['txtPesan']; include "conn.php"; mysql_select_db($db,$koneksi)or die(mysql_error()); $query=mysql_query("update buku_tamu set id = '$id', nama = '$nm', email = '$em', pesan = '$ps' where id = '$id' ",$koneksi)or die(mysql_error()); echo "<meta http-equiv='refresh' content='0;url=lihatTamu.php?Data tamu telah di update.'>"; ?> </body>

Page 7: Web Programming - DB Buku Tamu

Andriansyah Doni

w w w . f r e a k - k u t u o n l i n e . c o m

Page 7

Tambahkan file hapusTamu.php untuk menghapus data tamu. Script program hapusTamu.php <?php include "conn.php"; mysql_select_db($db,$koneksi)or die(mysql_error()); $query=mysql_query("delete from buku_tamu where id='$_GET[id]'"); echo "<meta http-equiv='refresh' content='0;url=lihatTamu.php?Data tamu berhasil di hapus.'>"; ?> Sekarang lihat tabel buku tamu didalam database dbBerita, maka hasilnya sebagai berikut.

Gambar 6. Tabel buku_tamu.