Here’s a easy way to create 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:
echo "<form action='index.php?act=add' method='post'>"
.”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’></textarea>
”
.”<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")
{ // If act is "add" like this file.php?act=add display the content
$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)
{ // If database has been successfully selected gather info and post it
$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)
VALUES ('".$name."', '".$email."', '".$website."',
'".$message."', '".$ip."', '".$time."')";
// 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:
$sqlCon = mysql_connect("localhost", "root", "bananaman"); // Connect to database
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>”
.”<td align=’right’>”.date(“H:i j F Y”, $info['date']).”</td>”
.”</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!







I wanted to share my links with you. But I don’t know how to put a link here. So, I’ll try to do it the ways I know.
So
I wish you never stop! This is one of the best blogs Ive at any time study. Youve gained some mad skill listed here, man. I just wish which you dont get rid of your design because youre certainly 1 from the coolest bloggers around. Please preserve it up because the online requirements another person like you spreading the word.
You certainly have some agreeable opinions and views. Your blog provides a fresh look at the subject.
Really interesting article and quality read. Thanks for coming up with a really unique add
You know, sometimes I think that few of us think same,and it dissapoints me.
Hi.your website it is good,thank you for posting Message
Hello, this is my first time i visit here. I found so many interesting in your blog especially on how to determine the topic. keep up the good work.
Of course, what a great site and informative posts, I will add backlink – bookmark this site? Regards, Reader
Great read. Thanks for the info!
I’ve just started off a blog, the knowledge you give on this site has aided me extremely. Thank you for all your time & work.
I REALLY liked your post and blog! It took me a minute bit to find your site…but I bookmarked it. Would you mind if I posted a link back to your post?
Good! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?
Thanks for posting. Good to see that not everyone is using RSS feeds to build their blogs
Awesome post. I so good to see someone taking the time to share this information
Great read. Thanks for the info!
I was just having a conversation over this I am glad I came across this it cleared some of the questions I had.
I’ve just started off a blog, the knowledge you give on this site has aided me extremely. Thank you for all your time & work.
Interesting read, perhaps the best article iv’e browse today. We learn everyday cheers to you!
I would like to say “wow” what a inspiring post. This is really great. Keep doing what you’re doing!!
Thanks for posting. Good to see that not everyone is using RSS feeds to build their blogs
Intriguing post. I have been searching for some good resources for solar panels and discovered your blog. Planning to bookmark this one!