Friday, July 6, 2012

Membuat Buku Tamu Sederhana dari PHP

            Membuat Buku Tamu Sederhana dari PHP

Rocky Christmas Tarigan

Buat Databasenya berikut rinciannya

 

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 ;

  • Membuat file guestbook.php

Tampilan dari file guestbook.php

01<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
02<tr>
03<td><strong>Test Sign Guestbook </strong></td>
04</tr>
05</table>
06<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
07<tr>
08<form id="form1" name="form1" method="post" action="addguestbook.php">
09<td>
10<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
11<tr>
12<td width="117">Name</td>
13<td width="14">:</td>
14<td width="357"><input name="name" type="text" id="name" size="40" /></td>
15</tr>
16<tr>
17<td>Email</td>
18<td>:</td>
19<td><input name="email" type="text" id="email" size="40" /></td>
20</tr>
21<tr>
22<td valign="top">Comment</td>
23<td valign="top">:</td>
24<td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
25</tr>
26<tr>
27<td>&nbsp;</td>
28<td>&nbsp;</td>
29<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
30</tr>
31</table>
32</td>
33</form>
34</tr>
35</table>
36<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
37<tr>
38<td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td>
39</tr>
40</table>
  • Membuat file addguestbook.php
01<?php
02$host="localhost"; // Host name
03$username=""; // Mysql username
04$password=""; // Mysql password
05$db_name="test"; // Database name
06$tbl_name="guestbook"; // Table name
07
08// Connect to server and select database.
09mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
10mysql_select_db("$db_name")or die("cannot select DB");
11
12$datetime=date("y-m-d h:i:s"); //date time
13
14$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
15$result=mysql_query($sql);
16
17//check if query successful
18if($result){
19echo "Successful";
20echo "<BR>";
21echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
22}
23
24else {
25echo "ERROR";
26}
27
28mysql_close();
29?>
  • Membuat file viewguestbook.php
01<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
02<tr>
03<td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td>
04</tr>
05</table>
06<br>
07
08<?php
09
10$host="localhost"; // Host name
11$username=""; // Mysql username
12$password=""; // Mysql password
13$db_name="test"; // Database name
14$tbl_name="guestbook"; // Table name
15
16// Connect to server and select database.
17mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
18mysql_select_db("$db_name")or die("cannot select DB");
19
20$sql="SELECT * FROM $tbl_name";
21$result=mysql_query($sql);
22
23while($rows=mysql_fetch_array($result)){
24?>
25<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
26<tr>
27<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
28<tr>
29<td>ID</td>
30<td>:</td>
31<td><? echo $rows['id']; ?></td>
32</tr>
33<tr>
34<td width="117">Name</td>
35<td width="14">:</td>
36<td width="357"><? echo $rows['name']; ?></td>
37</tr>
38<tr>
39<td>Email</td>
40<td>:</td>
41<td><? echo $rows['email']; ?></td>
42</tr>
43<tr>
44<td valign="top">Comment</td>
45<td valign="top">:</td>
46<td><? echo $rows['comment']; ?></td>
47</tr>
48<tr>
49<td valign="top">Date/Time </td>
50<td valign="top">:</td>
51<td><? echo $rows['datetime']; ?></td>
52</tr>
53</table></td>
54</tr>
55</table>
56<BR>
57<?
58}
59mysql_close(); //close database
60?>

0 comments:

Post a Comment

KOMENTAR