HomePHP 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.
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:
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:
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:
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.