Headlines News :

Advertise

Home » » Membuat Guestbook Sederhana dengan phpmysql

Membuat Guestbook Sederhana dengan phpmysql

Written By Susanto Wibowo on Minggu, 17 Juli 2011 | 04.06

Studi Kasus : Membuat guestbook sederhana dengan PHP Mysql
Kebutuhan : Webserver Packages, already installed.
Ok, langsung praktik saja.
Step 1 : Persiapkan Database
  1. Buat database dengan nama db_tutorial
  2. Siapkan tabel dengan nama tb_guestbook, dengan struktur tabel seperti gambar dibawah ini.
  3. Done!
Step 2 : Persiapkan Folder Kerja
  1. Buat folder dengan nama tutorphp dalam document root anda
  2. Simpan semua file dalam tutorial ini dalam folder tersebut.
Step 3 : Membuat script koneksi ke Database
Ketikkan script berikut,

<?php
$host = "localhost";
$user = "root";//adjust according to your mysql setting
$pass = ""; //adjust according to your mysql setting, i use no password here
$dbName = "db_tutorial";
mysql_connect($host, $user, $pass);
mysql_select_db($dbName)
or die ("Connect Failed !! : ".mysql_error());
?>
  1. simpan dengan nama connect.php
Step 4 : Membuat form guestbook
Ketikkan script berikut,

<h1>Guestbook</h1>
<form method="post" action="savegb.php">
<table>
<tr><td>Your Name</td><td></td></tr>
<tr><td>Your Email</td><td></td></tr>
<tr><td>Your Comment</td><td><textarea name="comment"></textarea></td></tr>
<tr><td></td><td></td></tr>
</table>
</form>

  1. simpan dengan nama formguestbook.php
Step 5 : Membuat script pemrosesan dan menampilkan guestbook

Ketikkan script berikut,

<?php
include 'connect.php';

$name = $_POST['name']; //get the name value from form
$email = $_POST['email']; //get the email value from form
$comment = $_POST['comment']; //get the comment value from form

//query to save to database
$q1 = "INSERT into tb_guestbook value ('','$name','$email','$comment')";
$result1 = mysql_query($q1);

//query to get data from database
$q2 = 'SELECT * FROM tb_guestbook order by id desc';
$result2 = mysql_query($q2);

//show the data from database
while ($record = mysql_fetch_array($result2)) {
echo "<table>";
echo "<tr><td>Name</td><td>:</td><td>".$record['name']."</td></tr>";
echo "<tr><td>Email</td><td>:</td><td>".$record['email']."</td></tr>";
echo "<tr><td>Comment</td><td>:</td><td>".$record['comment']."</td></tr>";
echo "</table>";
echo "<br/>";
}
?>

  1. simpan dengan nama savegb.php
  2. untuk penjelasan script, lihat komentar pada script :D
Step 6 : Testing Code
  1. Pergi ke http://localhost/tutorphp/formguestbook.php. Anda akan melihat form seperti dibawah. Isikan guestbook tersebut (misal seperti pada gambar dibawah)
  2. Klik tombol save dan anda akan melihat hasilnya
OK. Haaaaaaaapppy Coding yah gan..! :D
Share this article :

0 komentar:

Speak up your mind

Tell us what you're thinking... !

 
Support : Creating Website | Johny Template | Mas Template
Proudly powered by Blogger
Copyright © 2011. Media Informasi Online - All Rights Reserved
Template Design by Creating Website Published by Mas Template