Leakbali
w3 Tutorial, Web Tutorial
Switch to English Bahasa Indonesia 
Register   Login

Source Code  


Share

Simple Guestbook 

Here is a simple Guestbook with PHP and MySQL database. We learn how to create connection to MySQL database with PHP. First create a database called "myweb", then we create table called "guestbook".

Create database

CREATE DATABASE `myweb` ;


Create table

CREATE TABLE `guestbook` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 30 ) NOT NULL ,
`email` VARCHAR( 100 ) NOT NULL ,
`comment` TEXT NOT NULL ,
`date` DATETIME NOT NULL
);


After create database and table called guestbook, we create an HTML form:
<html>
<head><title>Simple Guestbook</title>
</head>
<body>
		<?php
		//parameter koneksi ke database
		$namaserver = "localhost";
		$username = "root";
		$password = "";
		$namadatabase = "myweb";
		//koneksi ke database
		mysql_connect($namaserver, $username, $password);
		//memilih database
		mysql_select_db($namadatabase);
		//parameter post
		$mode = $_GET['mode'];
		$id = $_GET['id'];
		if($mode == "hapus"){
			//menghapus data dari table
			mysql_query("DELETE FROM bukutamu WHERE id = '".$id."'");
			echo "<script>localhost.href='index.php';</script>";
		}
		if($mode == "ubah"){
			//memilih data dari table bukutamu
			$_u = mysql_query("SELECT *FROM bukutamu WHERE id = '".$id."'");
			$u = mysql_fetch_array($_u);
			$nama = $u['nama'];
			$email = $u['email'];
			$komentar = $u['komentar'];
		}else{
			$nama = $_POST['nama'];
			$email = $_POST['email'];
			$komentar = $_POST['komentar'];
		}
		$tombol = $_POST['tombol'];
		$ubah = $_POST['ubah'];
		
		if(isset($ubah)){
			//merubah data pada database
			$a = "UPDATE bukutamu SET nama = '".$_POST['nama']."', email = '".$_POST['email']."', komentar = '".$_POST['komentar']."' WHERE id = '".$_REQUEST['id']."'";
			mysql_query($a);
			echo mysql_error();
		}
		
		if(isset($tombol)){
			//menyimpan kedatabase
			$a = "INSERT INTO bukutamu (nama, email, komentar) VALUES ('$nama','$email','$komentar')";
			mysql_query($a);
			echo mysql_error();
		}
		?>
		<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
		<input type="hidden" name="id" value="<?php echo $id;?>" />
		<table id="bukutamu">
		<tr><td class="left">Nama</td><td class="right"><input type="text" name="nama" value="<?php echo $nama;?>" /></td></tr>
		<tr><td class="left">Email</td><td class="right"><input type="text" name="email" value="<?php echo $email;?>" /></td></tr>
		<tr><td class="left">Komentar</td><td class="right"><textarea name="komentar"><?php echo $komentar;?></textarea>
		</td></tr>
		<tr><td></td><td>
		<?php if($mode == "ubah"){?>
		<input type="submit" name="ubah" value="Ubah" /> 
		<?php }else{?>
		<input type="submit" name="tombol" value="Kirim" />
		<?php }?>		
 </td></tr>
		</table>
		</form>
		<fieldset>
	<legend>Guestbook</legend>
	<ul>
	<?php
	//menampilkan data dari table bukutamu
	$bta = mysql_query("SELECT *FROM bukutamu");
	echo "Total Pengunjung : ".$total = mysql_num_rows($bta);
	while($bt = mysql_fetch_array($bta)){
	?>
	<li><?php echo $bt['nama'];?>, <?php echo $bt['email'];?> [<a href="index.php?mode=ubah&id=<?php echo $bt['id'];?>">Ubah</a>][<a onclick="return confirm('Apakah data mau dihapus?');" href="index.php?mode=hapus&id=<?php echo $bt['id'];?>">Delete</a>]
	<br />
	<?php echo $bt['komentar'];?></li>
	<?php }?>
	</ul>
	</fieldset>
</body>
</html>


In this example, we show how to interact with MySQL database and request to server by a command INSERT, UPDATE, DELETE and SELECT.
Download
Post by : Oka Dayendra
Post on : 06 June 2011
File : gbook.zip, size : 0 bytes
Title : Simple Guestbook
Category : PHP
Source : http://www.leakbali.com
Comment (2)
Name *
Email *
Website
example : http://www.google.com or http://www.yourname.com
Comment *
Security Code *


browser Unknown 4.0 Unknown Platform 109.254.49.7 UKRAINE
Alex Post on 27-07-2011
Have nice day! Much useful information, nice design, but this color hurts your eyes. 蒻
browser Mozilla Firefox 5.0 Windows 7 Platform 114.79.58.219 INDONESIA
W3 Administrator Post on 02-08-2011
Thank you for your suggestion..

Related Articles

References

About Us

Home
About Us
Contact Us
Sitemap

Tools

Google PageRank
Alexa Rank
Keywords Density

Accounts

Register Account
Login
Can't Login?
Valid XHTML 1.0 TransitionalValid CSS!
2006 - 2014 © Leakbali.com - Free Web Tutorial, Free Web Articles, Web Sharing, Source Codes, Web References