Home arrow PHP arrow Page 10 - Building a Quick and Easy Tag Board

Ensuring Data Submitted Does Not Fail - PHP

Tag boards enable users to leave a short message on your site without having to go through the trouble of registering. From a development point of view, they are actually rather simple to develop. In this article we will create a quick and easy tag board for any web site. We will be taking advantage of the php and MySQL technologies.

  1. Building a Quick and Easy Tag Board
  2. Did Anyone Say MySQL?
  3. Our Tag Board Script
  4. What Makes Our Tag Board Tick?
  5. Our Tag Board Functions!
  6. Retrieval and Sorting of Tags
  7. Results
  8. doBoard() Function
  9. doInsert() Function
  10. Ensuring Data Submitted Does Not Fail
  11. Inserting into the Tag Board
By: Haiden Taylor
Rating: starstarstarstarstar / 120
November 03, 2004

print this article



Next we are adding slashes to the data submitted by the user through the form. This ensures that any characters that could cause the MySQL query to fail are escaped. The code we used to do this is detailed below:

$__name = addslashes($__name);
$__url = addslashes($__url);
$__entry = addslashes($__entry);

Before I continue I would just like to mention that the variables $__name, $__entry and $__url have all been fed to the function by the following code:

doInsert($_POST['__name'], $_POST['__entry'], $_POST['__url']);

which resides in our switch statement. This may seem quite obvious to most of us, but it is still good to keep this in mind, as if you changed the field name of one of our input fields and didn’t replicate those changes here, you could find yourself pulling your hair out in no time.

Now we will move on to the following code:

if (empty($__name))
<p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">Name Field Is Required</p>
<meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
} elseif (empty($__entry))
<p style="font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF">Entry Field Is Required</p>
<meta http-equiv="refresh" content="5;url='board.php?do=Tags'">
} else {

[Code removed, as there is no need to replicate it again]


This will check to see if all the required fields have been filled out (name and entry). As you can see, the script first checks whether the variable is empty; if the variable is empty an error message is displayed and then five seconds later the page in the Inline Frame is refreshed to display the tags. The data the user entered into the other fields’ remains in tact.

As you can see we have used an elseif statement here. It works in the same method as a standard if / else statement except it allows more than one statement. In our case, if the $__name variable is empty the error will be displayed, if it is not empty, we will then check if the $__entry variable is empty, if it is, display an error, if not, proceed to enter the tag into the database.

We will now have a look at the code which is executed if the $__name and $__entry variables are not empty. This code is below:

} else {

$stSql = "INSERT INTO tagboard(tagId, tag_name, tag_url, tag_entry, tag_date) values('', '$__name', '$__url', '$__entry', ".time().")";
$arrResult = @mysql_query($stSql);
if (!$arrResult) { echo "Query Failed - Unable to Retrieve tags - ".mysql_error()." - Line: __LINE__"; exit(); }

if (mysql_affected_rows() == 1)
<meta http-equiv="refresh" content="3;url='board.php?do=Tags'">
} else {
echo "Unable to Add Entry to Database";
<meta http-equiv="refresh" content="5;url='board.php?do=Tags'">

This snippet of code is similar to the code used to retrieve the tags from the database. There are several obvious differences, which we will look into next.

>>> More PHP Articles          >>> More By Haiden Taylor

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: