Kebutuhan : Webserver Packages, already installed.
Ok, langsung praktik saja.
Step 1 : Persiapkan Database
- Buat database dengan nama db_tutorial
- Siapkan tabel dengan nama tb_guestbook, dengan struktur tabel seperti gambar dibawah ini.
- Done!
- Buat folder dengan nama tutorphp dalam document root anda
- Simpan semua file dalam tutorial ini dalam folder tersebut.
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());
?>
- simpan dengan nama connect.php
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>
- simpan dengan nama formguestbook.php
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/>";
}
?>
- simpan dengan nama savegb.php
- untuk penjelasan script, lihat komentar pada script
- Pergi ke http://localhost/tutorphp/formguestbook.php. Anda akan melihat form seperti dibawah. Isikan guestbook tersebut (misal seperti pada gambar dibawah)
- Klik tombol save dan anda akan melihat hasilnya
0 komentar:
Speak up your mind
Tell us what you're thinking... !