Web Design Tutorials: Photoshop, HTML, Flash, PHP

PHP Tutorial – Guestbook

This is a beginner tutorial, but it is assumed that you already know some HTML and CSS. The tutorial will, kind of describe and explain my workflow in creating a guest book application. I will also explain the important segments of the code. Finished project can be downloaded from the link given at the end. If you”ve got a suggestion or a question, post a comment. Furthermore, I am sure this application needs a lot of improvements so your feedback is appreciated.

This is the organization of my project folder so all paths are relative to this structure.

folder_layout

Planning:

Defining what you want before you start something is an essential first step. So, our guest book application will have the following features:

  • The 3 user input fields “Name”, “Email” and “Comment”.
  • A database to store the data, in our case “mysql”.
  • A section where, already posted guest messages will appear.

All user input must be filtered for efficiency and security but that will be dealt in another tutorial together with making the application compatible with the lame Internet Explorer!!!

Let”s get on with it. Start by designing your page in Photoshop or whatever graphics software you use. I”ve designed a simple webpage using photoshop.

webpage_layout

Database Setup:

Code assumes that already a database named “guest book” exists with a table named “entries”. Table has four fields:

  • “ID”: This is the primary key. Set it to auto increment to make it unique among each database entry.
  • “Name”: The name of the visitor.
  • “Email”: Email of the visitor.
  • “Comment”: The actual comment posted by a particular individual.

Coding the layout:

index.php file:

1.<html>
2.<head>
3.<title>Guest Book Demo</title>
4.<link rel="stylesheet" href="/./css/layout.css" type="text/css" />
5.</head>

This is the start of our webpage. Notice that I have included a link to our CSS stylesheet in the header section.

01.<body>
02.<?php
03.session_start();
04.//Database Login Information
05.$_SESSION[''host''] = "localhost";
06.$_SESSION[''user''] = "root";
07.$_SESSION[''pass''] = "";
08.$_SESSION[''name''] = "guestbook";
09.?>

Start a session with variables containing information about the mySQL database. I”m running mySQL on WAMP so “root” is the default username and empty password field. Change them if needed.

1.<div><p>DEMO GUESTBOOK APPLICATION</p><h1>GUEST BOOK</h1></div>
2.<div>
3.<div>{</div>

Last line above displays a “{“. This is just a design element and nothing else.

Now code the form. Set the form “action” to “processor.php” which will process the user input and make an entry in our table.

01.<table border="0">
02.<form action="processor.php" method="post">
03.<tr>
04.<td><p>Name:</p></td>
05.<td><p><input type="text" name="name" size="36" /></p></td>
06.</tr>
07.<tr>
08.<td><p>Email:</p></td>
09.<td><input type="text" name="email" size="36"/></td>
10.</tr>
11.<tr>
12.<td><p>Comment:</p></td>
13.<td><textarea cols="28" rows="5" name="comment" ></textarea></td>
14.</tr>
15.<tr>
16.<td colspan="2"><input type="submit" name="submit" value="Post" /></td>
17.</tr>
18.</form>
19.</table>

This creates the 3 field form by using a 2×3 table. To enable the user to input comments which are multi-line text <textarea> tag is used instead of <input> tag.

Displaying previous comments:

This is in combination with the CSS stylesheet so have a look at it while following this code.

1.<div>
2.<div>
3.<?php
4.$connection = mysql_connect($_SESSION[''host''], $_SESSION[''user''], $_SESSION[''pass''], $_SESSION[''name'']) or die (mysql_error());
5.mysql_select_db ($_SESSION[''name'']) or die(mysql_error());

“mysql_connect ()” opens a connection with the database. Login information is provided in the 3 arguments for the function. “mysql_select_db” will choose which database to access information from. Incase these functions fail, “die (mysql_error())” will display an error on the page and stop further execution of the script.

1.$query = "SELECT * FROM entries ORDER BY ID DESC";

This query will fetch all previous guestbook entries from the “entries” table in descending (“DESC”) order by ID field. It means that the latest comments will be on top of the table.

1.$result = mysql_query ($query) or die (mysql_error());

“mysql_query ()” function fetches the table using the above query.

1.while ($row = mysql_fetch_array ($result)) {
2.echo ">> - <b>{$row[''Name'']}</b> ({$row[''Email'']}) <b>says</b> \"{$row[''Comment'']}\" </p><br>";
3.}

“mysql_fetch_array ()” traverses the table row by row fetched by “msql_query ()” and the “echo” statement displays the row formated .

1.mysql_close ($connection) or die (mysql_error ());
2.?>
3.</div>
4.</div>
5.</div>

Add a simple footer at the end of the page.

1.<div>Copyright (C) 2011 <a href="/www.pinchofdesign.com">Pinch of Design</a>. All rights reserved.</div>
2.<?php
3.session_destroy();
4.?>
5.</body>
6.</html>

I won”t explain the css part. It is very simple. Still if you have any problems/questions regarding it, leave a comment.

Processing the user input:

The form we built on the index page posts the user information to this processing script (processor.php).

1.<?php
2.session_start();

This will make available the DB connection information variables from the main page to this page.

1.$connection = mysql_connect($_SESSION[''host''], $_SESSION[''user''], $_SESSION[''pass''], $_SESSION[''name'']) or die (mysql_error());

Again connect to the database using the login information.

1.mysql_select_db ($_SESSION[''name'']) or die(mysql_error());

Select the appropriate database.

1.$query = "INSERT INTO entries (ID, Name, Comment, Email) VALUES (NULL, ''{$_POST[''name'']}'', ''{$_POST[''comment'']}'', ''{$_POST[''email'']}'');";

Build a query to insert the new information entered by the user. In the above query string:

  • “entries” is the name of the table present in the database.
  • “ID”, “Name”, “Comment”, “Email” are the fields present in that table.
  • “$_POST” array contains new user input. Which can be accessed by using the form input name as the index of this array. Like “$_POST[''name'']” will access the user input in the input field named “name”. These user inputs are passed as values in the query to insert them in the database. “NULL” is sent against “ID” because it is an auto increment field so input is not needed.
1.mysql_query ($query) or die (mysql_error());

execute the above query.

1.mysql_close($connection);

and close the connection.

1.header ("Location: http://localhost/guestbook");
2.?>

And lastly redirect the user to our main page again.

Source: pinchofdesign.com

you may also like:

Leave a Reply

Powered by WordPress | Designed by Elegant Themes