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.


 CREATE TABLE IF NOT EXISTS `posts` (

 `name` varchar(100) NOT NULL,

 `email` varchar(100) NOT NULL,

 `website` varchar(100) NOT NULL,

 `message` text NOT NULL,

 `ip` varchar(25) NOT NULL,

 `date` varchar(50) NOT NULL

 ) 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:


<code><code>echo "php?act=add' method='post'>"</code></code>

."Name:
<input type='text' name='name' size='30' />
"

."Email:
<input type='text' name='email' size='30' />
"

."Website:
<input type='text' name='website' size='30' />
"

."Message:
<textarea cols='30' rows='8' name='message'>
"

."<input type='submit' value='Post' />"

."</form>";

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

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

if($_GET['act'] == "add") </code></code>

<code><code>{ // If act is "add" like this file.php?act=add display the content</code></code>

$sqlCon = mysql_connect("localhost", "root", "bananaman"); // Connect to database

if($sqlCon == true){ // If connection has been made then..

$sqlSel = mysql_select_db("guestbook", $sqlCon); // Select a database

if($sqlSel == true)

<code><code>{ // If database has been successfully selected gather info and post it</code></code>

$name = addslashes(htmlspecialchars($_REQUEST['name'])); // Takes "name" field from the form

$email = addslashes(htmlspecialchars($_REQUEST['email'])); // Takes "email" field from the form

$website = addslashes(htmlspecialchars($_REQUEST['website'])); // Takes "website" field from the form

$message = addslashes(htmlspecialchars($_REQUEST['message'])); // Takes "message" field from the form

$ip = $_SERVER['REMOTE_ADDR']; // This takes user's IP

$time = time(); // Get UNIX timestamp

// This will create a SQL query which inserts all that information into database

$sql = "INSERT INTO posts (name, email, website, message, ip)

<code><code>VALUES ('".$name."', '".$email."', '".$website."', </code></code>

<code><code>'".$message."', '".$ip."', '".$time."')";</code></code>

// This will process SQL query and performs inserting info to database

$query = mysql_query($sql);

if($query == true) { // If everything was correct ..

echo "Your post has been successfully posted!"; // Display a success message

}else { // But if there was something wrong ..

echo "There was something wrong."; // Display a fail message

// As you can see I have commented

// mysql_error() function. It is because

// if you want to see what went wrong with

// your SQL query. Displaying it is not very

// good idea because it's like a candy to

// hackers. So if you have fixed the problem

// make sure to add comment "//" in front of it

//echo mysql_error();

}

mysql_close($sqlCon); // Close connection to database

}else{

exit; // If database selection wasn't a success close script

}

}else{

exit; // If connecting to database wasn't a success close script

}

}

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

Step 3: Displaying Posts

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

Use this PHP code:


<code><code>$sqlCon = mysql_connect("localhost", "root", "bananaman"); // Connect to database</code></code>

if($sqlCon == true) { // If connecting was successful ..

$sqlSel = mysql_select_db("guestbook", $sqlCon); // select your database

if($sqlSel == true) { // If database selecting was successful then take info from it

// Select all records from database

$sql = "SELECT * FROM posts";

// Process your selection

$query = mysql_query($sql);

// This creates a table which contains all info

// of your posts

while($info = mysql_fetch_array($query)) {

echo "<table width='300' border='1'>"

."<tr>"

."<td>Posted by: <a href='".$info['website']."'>".$info['name']."</a> (<a href='mailto:".$info['email']."'>Email</a>)</td>"

."".date("H:i j F Y", $info['date']).""

."</tr>"

."<tr>"

."<td colspan='2'>".$info['message']."</td>"

."</tr>"

."</table>"

."
";

}

mysql_close($sqlCon); // Close database connection

}else { // If database selection wasn't successful ..

exit; // exit form the script

}

}else { // If connection to database wasn't success ..

exit; // exit from your script

}

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

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.