• home
  • forum
  • my
  • kt
  • download
  • Basic PHP/MySQL One-Page Guestbook Tutorial

    Author: 2009-04-17 11:17:58 From:

    Do you want to encourage your visitors to say hi when they visit your friendly Website? This tutorial is divided in 4 phases and will teach you how to create a simple PHP guestbook using only one PHP file

    We will create the PHP/MySQL Guestbook in four phases

    1. Setting up the database
    2. Creating the Guestbook form
    3. Coding the PHP script that will process the guestbook submissions
    4. Coding the PHP script that will display the guestbook entries



    1. Setting up the database


    The table's name will be guestbook. We are going to setup the following columns:

    • id - the unique identifier for a guestbook entry
    • name - the visitor's name
    • email - the visitor's email
    • comments - the message
    • datetimecreated - to record the date and time the visitor submitted an entry



    If you haven't created a database yet, using whatever interface you use for working with MySQL databases, you can execute the following command to create a database.

    CREATE DATABASE mywebsite;



    You can execute the following command to create our guestbook table.

    CREATE TABLE guestbook
    (
        id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        name TEXT,
        email TEXT,
        comments TEXT,
        datetimecreated DATETIME NOT NULL
    );



    2. Creating the Guestbook form


    <form name="guestbook" action="<?php echo $_SERVER['PHP_SELF'].' ?>" method="POST">
    <input type="text" name="name" />
    <input type="text" name="email" />
    <textarea name="comments" rows="10" cols="50" >
    </textarea>
    <input type="hidden" name="process" value="1" />
    <input type="submit" value="Send Message!"
    </form>



    We have 3 fields where the visitor can enter data and 1 hidden field with a preset value:

    • name: for the user to input his/her name
    • email: for the user to input his/her email address
    • comments: guestbook comments
    • process: is a hidden fields that will send a value of 1 when the user clicks the Send Message! button. We use this hidden field to let the PHP code know that the user has clicked on the button and that it should process the code that inserts the data in the database



    When the user clicks the Send Message! button, a $_POST array is created. $_POST is an array of variable names and values sent by the HTTP POST method. The $_POST variable is used to collect values from a form that uses the POST method.

    3. Coding the PHP script that will process the guestbook submissions


    if(isset($_POST['process']) and $_POST['process']==1)
    {
        $name = trim($_POST['name']);
        $email = trim($_POST['email']);
        $comments = trim($_POST['comments']);

        $query = 'INSERT INTO guestbook SET
             name = "'.$name.'",
             email = "'.$email.'",
             comments = "'.$comments.'",
             datetimecreated = NOW()';

        if(mysql_query($query))
        {
            echo '<p style="color:#00F;">Thank you for signing our guestbook!</p>';
        }
        else
        {
            echo '<p style="color:#C00;">Sorry, your entry could not be submitted.</p>';
        }
    }



    if(isset($_POST['process']) and $_POST['process']==1) allows us to process the code inside the if statement ONLY when the submit button has been clicked.

    We build the query using the values entered into the form and the NOW() MySQL function which returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' format.

    The msyql_query() function returns TRUE if the email was successfully sent, FALSE otherwise. If the function was executed successfully, a thank you message in blue letters will be displayed. If the function returns a FALSE value (the query was not successful), the code echos an error message in red letters.

    4. Coding the PHP script that will display the guestbook entries


    $query = 'SELECT * FROM guestbook ORDER BY datetimecreated DESC';
    $result = mysql_query($query);
    if(!$result)
    {
        echo '<p style="color:#C00;">Could not retrieve guestbook entries.</p>';
    }
    else
    {
        while($row = mysql_fetch_array($result))
        {
            echo '<p>';
            echo '<strong>'.$row['name'].'<strong><br />';
            echo $row['email'].'<br />';
            echo date('F j, Y', strtotime($row['datetimecreated'])).' at '.date('g:i a', strtotime($row['datetimecreated'])).'<br />';
            echo $row['comments'];
            echo '</p>';
        }
    }
    ?>



    The query will sort the results from newest to oldest by including the ORDER BY clause.

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Ad Management (6)
      Calendars (3)
      Chat Systems (8)
      Content Management (46)
      Cookies and Sessions (12)
      Counters (16)
      Database Related (36)
      Date and Time (15)
      Development (26)
      Discussion Boards (8)
      E Commerce (9)
      Email Systems (15)
      Error Handling (8)
      File Manipulation (38)
      Flash and PHP (6)
      Form Processing (25)
      Guestbooks (13)
      Image Manipulation (26)
      Installing PHP (7)
      Introduction to PHP (29)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (60)
      Networking (9)
      News Publishing (9)
      OOP (28)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (7)
      Postcards (1)
      Randomizing (15)
      Redirection (12)
      Searching (10)
      Security (32)
      Site Navigation (16)
      User Authentication (16)
      WAP and WML (7)
      Web Fetching (10)
      Web Traffic Analysis (15)
      XML and PHP (18)

    New

    Hot