Rabu, 17 Desember 2008

Membuat Guestbook dengan PHP

Bagi anda yang ingin belajar PHP, maka anda bisa mencoba membuat sebuah program sederhana yaitu buku tamu (guestbook). berikut langkah-langkahnya :

1. Buatlah database “guesbook”



CREATE TABLE `guestbook` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default ”,
`email` varchar(65) NOT NULL default ”,
`comment` longtext NOT NULL,
`datetime` varchar(65) NOT NULL default ”,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

2. Buat file guestbook.php







Test Sign Guestbook





























Name:
Email:
Comment:
  






View Guestbook


3. Buatlah file addguestbook.php



$host=”localhost”; // Host name
$username=”"; // Mysql username
$password=”"; // Mysql password
$db_name=”test”; // Database name
$tbl_name=”guestbook”; // Table name



// Connect to server and select database.
mysql_connect(”$host”, “$username”, “$password”)or die(”cannot connect server “);
mysql_select_db(”$db_name”)or die(”cannot select DB”);

$datetime=date(”y-m-d h:i:s”); //date time

$sql=”INSERT INTO $tbl_name(name, email, comment, datetime)VALUES(’$name’, ‘$email’, ‘$comment’, ‘$datetime’)”;
$result=mysql_query($sql);

//check if query successful
if($result){
echo “Successful”;
echo “
”;
echo “View guestbook”; // link to view guestbook page
}

else {
echo “ERROR”;
}

mysql_close();
?>

4. Buat file viewguestbook.php





View Guestbook | Sign Guestbook





$host=”localhost”; // Host name
$username=”"; // Mysql username
$password=”"; // Mysql password
$db_name=”test”; // Database name
$tbl_name=”guestbook”; // Table name

// Connect to server and select database.
mysql_connect(”$host”, “$username”, “$password”)or die(”cannot connect server “);
mysql_select_db(”$db_name”)or die(”cannot select DB”);

$sql=”SELECT * FROM $tbl_name”;
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
?>






























ID:
Name:
Email:
Comment:
Date/Time :



}
mysql_close(); //close database
?>

ayo semangat .....Ganbate kudasai!!!!!
!!

Tidak ada komentar: