PHP: Membuat Guest Book Sederhana

From OnnoWiki
Jump to navigation Jump to search

Sumber:

http://www.phpeasystep.com/phptu/15.html
http://www.phpeasystep.com/workshopview.php?id=16


Yang perlu dibuat

  • Table "guestbook" in database "test".
  • guestbook.php.
  • addguestbook. php.
  • viewguestbook.php


Membuat tabel guestbook

mysql -u root -p123456
create database test;
use test;
grant ALL on root.* to test@localhost;
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`)
) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


guestbook.php

Buku Tamu
<form id="form1" name="form1" method="post" action="addguestbook.php"> </form>
Nama : <input name="name" type="text" id="name" size="40" />
Email : <input name="email" type="text" id="email" size="40" />
Komentar : <textarea name="comment" cols="40" rows="3" id="comment"></textarea>
    <input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" />
<a href="viewguestbook.php">Lihat Buku Tamu</a>


addguestbook.php

<?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 "
"; // link to view guestbook page echo "<a href='viewguestbook.php'>View guestbook</a>"; } else { echo "ERROR"; } mysql_close(); ?>