1. Creating database
First, make table of database. Save the database with name database.sql
CREATE TABLE GuestBook { GuestID int (10) NOT NULL auto_increment ; GuestDate datetime NOT NULL default "0000-00-00 00:00:00" ; GuestFrom varchar (255) NOT NULL default " " ; GuestContent text ; PRIMARY KEY (GuestID) } TYPE=MyISAM |
2. Creating file db
Then we make a new file db.php. fille whose content are share function for connectivity to the database. Here the source code file db.php :
| <?php // Page db.php $db_hostname = "localhost"; //Server address can be change in the MySql $db_username = "username"; //Username can be change in the MySql $db_password = "password"; //Password can be change in the MySql $db_name = "dbname"; //Database name can be change in the MySql //Function to connect to the MySql server fungction connect_db() { global $db_hostname, $db_username, $db_password, $db_name; mysql_connect ($db_hostname, $db_username, $db_password); mysql_select_db ($db_name); } ?> |
3. Make home page GuestBook
Now we make the display face to fill the guestbook. In the script below, the emphasis on the use of php embedded in html script, so formnya maybe just simple. You can memmpercantik tampilanya in accordance with your creations.
Create a new file to be a display face based on isting code below, and save it as content_guestbook.php:
| // Page of content_guestbook.php <?php require_once "db.php"; $GuestDate=date ("Y-m-d H:m:s"); if($action=="content"){ connect_db(); $query=mysql_db_query ($dbname, "INSERT INTO GuestBook (GuestDate,GuestFrom,GuestContent) values ('$GuestDate','$GuestFrom','$GuestContent')"); if ($query){ echo "Data successfully added to database"; } else { echo "Data were not successfully added to database"; } } ?> <html> <head> <title>Creating Page Guestbook with PHP and Mysql</title> </head> <body> <form method="post" action="content_guestbook.php?action=load"> <table width="400" cellspacing="0" cellpadding="0" align="left"> <tr> <td align="center"><font face="arial" size="4">Page of GuestBook</font></td> </tr> <tr> <td align="left"><font face="arial" size="2">From</font></td> </tr> <tr> <td align="left"><input type="text" name="GuestFrom" value="<? echo "$GuestFrom";?>" size="40"></td> </tr> <tr> <td align="left"><font face="arial" size="2">Isi Guest Book</font></td> </tr> <tr> <td align="left"><textarea name="GuestContent" cols="40" rows="8"><? echo "$GuestContent"; ?></textarea></td> </tr> <tr> <td align="left"><input type="submit" value="Content Guest Book"> <input type="reset" value="Kosongkan"></td> </tr> </table> </form> </body> </html> |


0 comments:
Posting Komentar