how to make simple guestbook with php

Here in this PHP tutorial, we will give you code sample for, how to make simple guestbook with php. we given code sample with database schema. we explained every step. Here’s an easy way to create guestbook with php.

how to make simple guestbook with php

First you have to create database for it. If your database has been created add this table to database.

01CREATE TABLE IF NOT EXISTS `posts` (
02 
03`name` varchar(100) NOT NULL,
04 
05`email` varchar(100) NOT NULL,
06 
07`website` varchar(100) NOT NULL,
08 
09`message` text NOT NULL,
10 
11`ip` varchar(25) NOT NULL,
12 
13`date` varchar(50) NOT NULL
14 
15) ENGINE=InnoDB DEFAULT CHARSET=latin1;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Step 1: Creating A Form

First we have to create a form. to do this use the following PHP code:

01<code><code>echo "php?act=add' method='post'>"</code></code>
02 
03."Name:
04<input type='text' name='name' size='30' />
05"
06 
07."Email:
08<input type='text' name='email' size='30' />
09"
10 
11."Website:
12<input type='text' name='website' size='30' />
13"
14 
15."Message:
16<textarea cols='30' rows='8' name='message'>
17"
18 
19."<input type='submit' value='Post' />"
20 
21."</form>";

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Step 2: Add Info To Database

Now we have to add info to database. Do this using by this PHP code:

01if($_GET['act'] == "add") </code></code>
02 
03<code><code>{ // If act is "add" like this file.php?act=add display the content</code></code>
04 
05$sqlCon = mysql_connect("localhost""root""bananaman"); // Connect to database
06 
07if($sqlCon == true){ // If connection has been made then..
08 
09$sqlSel = mysql_select_db("guestbook"$sqlCon); // Select a database
10 
11if($sqlSel == true)
12 
13<code><code>{ // If database has been successfully selected gather info and post it</code></code>
14 
15$name addslashes(htmlspecialchars($_REQUEST['name'])); // Takes "name" field from the form
16 
17$email addslashes(htmlspecialchars($_REQUEST['email'])); // Takes "email" field from the form
18 
19$website addslashes(htmlspecialchars($_REQUEST['website'])); // Takes "website" field from the form
20 
21$message addslashes(htmlspecialchars($_REQUEST['message'])); // Takes "message" field from the form
22 
23$ip $_SERVER['REMOTE_ADDR']; // This takes user's IP
24 
25$time = time(); // Get UNIX timestamp
26 
27// This will create a SQL query which inserts all that information into database
28 
29$sql = "INSERT INTO posts (name, email, website, message, ip)
30 
31<code><code>VALUES ('".$name."''".$email."''".$website."', </code></code>
32 
33<code><code>'".$message."''".$ip."''".$time."')";</code></code>
34 
35// This will process SQL query and performs inserting info to database
36 
37$query = mysql_query($sql);
38 
39if($query == true) { // If everything was correct ..
40 
41echo "Your post has been successfully posted!"// Display a success message
42 
43}else // But if there was something wrong ..
44 
45echo "There was something wrong."// Display a fail message
46 
47// As you can see I have commented
48 
49// mysql_error() function. It is because
50 
51// if you want to see what went wrong with
52 
53// your SQL query. Displaying it is not very
54 
55// good idea because it's like a candy to
56 
57// hackers. So if you have fixed the problem
58 
59// make sure to add comment "//" in front of it
60 
61//echo mysql_error();
62 
63}
64 
65mysql_close($sqlCon); // Close connection to database
66 
67}else{
68 
69exit// If database selection wasn't a success close script
70 
71}
72 
73}else{
74 
75exit// If connecting to database wasn't a success close script
76 
77}
78 
79}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Step 3: Displaying Posts

Now posting part is done, now we're going to display our posts.

Use this PHP code:

01<code><code>$sqlCon = mysql_connect("localhost""root""bananaman"); // Connect to database</code></code>
02 
03if($sqlCon == true) { // If connecting was successful ..
04 
05$sqlSel = mysql_select_db("guestbook"$sqlCon); // select your database
06 
07if($sqlSel == true) { // If database selecting was successful then take info from it
08 
09// Select all records from database
10 
11$sql "SELECT * FROM posts";
12 
13// Process your selection
14 
15$query = mysql_query($sql);
16 
17// This creates a table which contains all info
18 
19// of your posts
20 
21while($info = mysql_fetch_array($query)) {
22 
23echo "<table width='300' border='1'>"
24 
25."<tr>"
26 
27."<td>Posted by: <a href='".$info['website']."'>".$info['name']."</a> (<a href='mailto:".$info['email']."'>Email</a>)</td>"
28 
29."".date("H:i j F Y"$info['date']).""
30 
31."</tr>"
32 
33."<tr>"
34 
35."<td colspan='2'>".$info['message']."</td>"
36 
37."</tr>"
38 
39."</table>"
40 
41."
42";
43 
44}
45 
46mysql_close($sqlCon); // Close database connection
47 
48}else // If database selection wasn't successful ..
49 
50exit// exit form the script
51 
52}
53 
54}else // If connection to database wasn't success ..
55 
56exit// exit from your script
57 
58}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

And you are done with your script. It should look like this:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thank you!

3 thoughts on “how to make simple guestbook with php”

Comments are closed.