Slash Coding

  • Android Development
  • CSS
  • Subscribe
  • About Us
  • Contact Us
You are here: Home / Web Development / PHP / Create a Simple Guestbook using PHP

Create a Simple Guestbook using PHP

By Aneesh Bhatnagar

Recently while working on a free-lance project, a Marriage Website for one of my friend, I happened to create a Guestbook for that website. After which, I decided to write a tutorial on how to create a guestbook in case someone might need it. If you do not know what a guestbook is, in real life a guestbook is basically a diary kept at various places and for various occasions where people can leave their wishes or feedback for any event. In a similar way,Β  an online guestbook is a service, which enables you to allow your visitors to leave comments and feedback for any event or any product, which is visible to the public.

Guestbook Screenshot

Well, developing a Guestbook is not a difficult task. It is pretty simple if you know what you are required to do! (Basically, for any problem, if you know what you are supposed to do, it’s pretty easy!). Let me “pen down” the basic steps involved in development of a guestbook.

  • A user is displayed a form, which he or she must fill out.
  • A confirmation message is displayed to the user when the comment is saved in the database.
  • A user can browse through all the comments posted till now on the website.

To solve this simple problem, we will make use of PHP and as always, I would be using my favorite text editor, Notepad++. If you don’t use Notepad++, I highly advise you to use it. Read more about it here. Also, we will be required to use a database to store the comments and information about the user. We will use a MySQL Database.

  • Using Notepad++ for enhanced Programming Experience

Guestbook in PHP

Let’s get started with the process of building our very own guestbook.

Guestbook Form

In this code, we basically redirect the form to a PHP page on our server named “addcomment.php” and then we do the main programming part there.

  1. Create a new HTML page, and in the body tag of the page, add the following code.
    </pre>
    <form action="addcomment.php" method="post" name="guest">Name:Β Β Β  <input type="text" name="name" />
    
    Email: <input type="text" name="email" />
    
    Message:
    
     <textarea cols="50" name="message" rows="10"> </textarea>
    
    <input type="submit" value="Sign this in the Book" /></form>
    <pre>
    
  2. Now, if you want to add a validation check for the name and email fields, add the following JavaScript code to your head tag.
    <script type="text/javascript">// <![CDATA[
    function Validate()
    {
    var x=document.forms["guest"]["email"].value;
    var y=document.forms["guest"]["name"].value;
    if(y==null || y=="")
    {
    alert("Please enter your Name! ");
    return false;
    }
    if(x==null || x=="")
    {
    alert("Please enter your email address!");
    return false;
    }
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 ||="" dotpos+2="">=x.length)
    {
    alert("Not a valid e-mail address");
    return false;
    }
    else
    return true;
    }
    </atpos+2>
    // ]]></script>
    
  3. Then add the following attribute to the form tag.
    onsubmit="return Validate();"
    
  4. The complete Form Tag will now look somewhat like this.
    </pre>
    <form action="addcomment.php" method="post" name="guest" onsubmit="return Validate();">

The SQL Part

We now need to create a MySQL table in a database to save our data entered by the user. To do this, we need to run the following query on our MySQL Server. On our server, we had to use phpMyAdmin to create a table in our database.


CREATE TABLE guestbook(
id int(5) NOT NULL auto_increment,
name varchar(60) NOT NULL default ' ',
email varchar(60) NOT NULL default ' ',
message text NOT NULL,
Primary key(id)
);

The PHP Files

Now let’s get creating our PHP files. We need one file which will add the comment to the user and then display a confirmation or error message and one file which will display all the comments stored in our database. First let’s make the addcomment.php file.

  1. Create a new PHP file and paste the following code in there.
    <?php
    $host="localhost"; //Add your SQL Server host here
    $user="root"; //SQL Username
    $pass=""; //SQL Password
    $dbname="slashcoding"; //SQL Database Name
    $con=mysqli_connect($host,$user,$pass,$dbname);
    if (mysqli_connect_errno($con))
    {
    echo "<h1>Failed to connect to MySQL: " . mysqli_connect_error() ."</h1>";
    }
    $name=$_POST['name'];
    $email=$_POST['email'];
    $message=$_POST['message'];
    $sql="INSERT INTO guestbook(name,email,message) VALUES('$name','$email','$message')";
    if (!mysqli_query($con,$sql))
    {
    die('Error: ' . mysqli_error($con));
    }
    else
    echo "Values Stored in our Database!";
    mysqli_close($con);
    ?>
    
  2. Save this file as addcomment.php in the same folder as the above created HTML file.
  3. Now, again create a new PHP file, which will display the comments and the names of the people to the public. Name this file “guestbook.php“.
  4. Add the following code to the file.
    <?php
    $host="localhost"; //Add your SQL Server host here
    $user="root"; //SQL Username
    $pass=""; //SQL Password
    $dbname="slashcoding"; //SQL Database Name
    $con=mysqli_connect($host,$user,$pass,$dbname);
    if (mysqli_connect_errno($con))
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT name,message FROM guestbook");
    while($row = mysqli_fetch_array($result))
    { ?>
    <h3><?php echo $row['name']; ?></h3>
    <p><?php echo $row['message']; ?></p>
    <?php }
    mysqli_close($con);
    ?>
    
  5. Be sure to change the variables (host, username, password, database, and table) in both the above created PHP files.

Download Source Code

Well, that’s it. You are ready to fire it up with some CSS and set it live on your website. This was a quick and easy tutorial for beginners. I hope I enabled you to create a Guestbook for your website. Keep subscribed to Slash Coding for more such updates. You can subscribe via RSS Feeds, Liking our Facebook Page or by Following us on Twitter. It’s your pick! πŸ˜‰

Did you enjoy this article?
Subscribe to our email alerts when we post something new so you don't miss out.

About Aneesh Bhatnagar

Aneesh Bhatnagar is a freelance web developer, who specializes in front-end website development with HTML5, CSS3 and jQuery. He believes in transforming the entire web into a Responsive Web. He also specializes in use of Bootstrap framework to get websites up and running.

Comments

  1. Rabbit says

    March 20, 2014 at 10:44 pm

    Superb post but I was wanting to know if you could write a litte more on this topic?

    I’d be very thankful if you could elaborate a little bit more.
    Appreciate it!

    • Aneesh Bhatnagar says

      March 20, 2014 at 11:34 pm

      Hey! Thank you for the feedback. What more would you like me to elaborate upon?

  2. Tamika says

    April 4, 2014 at 5:38 am

    I have ffun with, result in I foynd exactly what I
    used to be looking for. You have ended my 4 day long hunt!
    God Bless youu man. Have a great day. Bye

    • Aneesh Bhatnagar says

      April 4, 2014 at 11:10 am

      I’m glad this tutorial helped you! πŸ™‚

  3. Vasiliy says

    April 26, 2014 at 1:23 am

    Well, that’s it. You are ready to fire it up with some CSS and set it live on your website???

    • Aneesh Bhatnagar says

      April 29, 2014 at 3:34 pm

      Yes! That’s all. Just style your guestbook in the way you want! πŸ˜€

  4. Vasiliy says

    April 26, 2014 at 1:24 am

    Values Stored in our Database!

  5. Vasiliy says

    April 26, 2014 at 1:26 am

    how to make that the message appeared on a site?

    • Aneesh Bhatnagar says

      April 29, 2014 at 3:36 pm

      If you followed the tutorial correctly, that message should be displayed everytime the data is saved in the Database!

  6. EnjeruNyu says

    April 29, 2014 at 11:26 pm

    Is it possible to add a function to upload pictures to that?

    • Aneesh Bhatnagar says

      April 30, 2014 at 4:10 pm

      Yes it is possible to upload pictures. That is saved for an upcoming tutorial. Keep subscribed to Slash Coding so that you do not miss any updates from us! πŸ™‚

  7. thando says

    May 22, 2014 at 8:19 am

    How do i make the messages appear on the website. the idea is for people to post messages on my website.

    • Aneesh Bhatnagar says

      May 24, 2014 at 4:26 pm

      Hey Thando,
      Under the PHP Files section of this article, have a look at step no. 3 and 4. They show you how to display the contents of your Guestbook to the user.

  8. Regina says

    August 16, 2014 at 11:51 am

    Interesting topic.

    • Aneesh Bhatnagar says

      August 16, 2014 at 8:30 pm

      I’m glad you liked it! πŸ™‚

  9. Jolly says

    May 21, 2015 at 10:12 pm

    Hi!

    Thanks for this wonderful tutorial. I was wondering if you have similar tutorial for PHP5. Please let me know.

    • Aneesh Bhatnagar says

      May 22, 2015 at 8:43 am

      Hey Jolly,
      You’re welcome. But unfortunately I don’t have a similar tutorial for PHP5.

Search Slash Coding

Follow Us

RSS Feeds Facebook Twitter Follow Google+

Categories

  • Android Development
  • C++
  • Developer Tips
  • Slash Coding
  • Web Development
    • CSS
    • JavaScript
    • jQuery
    • PHP

Recently Published

  • Moving Ahead from Front End and Android Development
  • How to Export a Project from Android Studio
  • What is jQuery? Let’s Answer this Common Question today!
  • Mobile App Prototyping : Pixate Studio
  • How to Create a Countdown Timer using jQuery

Subscribe to Email Alerts

Eager to learn more from Slash Coding?
Sign up for daily email updates when anything new comes up. Get it straight in your inbox.

Follow Us on Social Media

RSS Feeds Facebook Twitter Follow Google+

Copyright © 2025 · Slash Coding · Powered by the Genesis Framework
Sitemap | Privacy Policy | About | Contact

×
Get Notified of the new Tutorials when they are posted!
Never miss any article update from Slash Coding. Subscribe to email alerts today!