Building a Quick and Easy Tag Board

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.

Introduction

Tag boards enable users to leave a short message on your site without having to go through the trouble of registering. They are the new age guestbook, and apart from giving the visitors to your site a voice, they are sure to make your friends envious too.

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, and a basic knowledge of both is assumed.

Please note: Developing an administration area and securing it is out of the scope of this article. Therefore we will not be incorporating the ability to delete/edit tags on our tag board.

What is a Tag Board?

Tag boards, in most cases, usually consist of a content area (normally an iframe) and an input area where users can put their message, name and a URL. An example of this can be seen below:

 

Quick and Easy Tag Board


Our tag board in this case will be using a MySQL database to store its information. I will be developing the MySQL table so it will be easy for any user with an intermediate knowledge of using php and MySQL can easily develop the appropriate administration functionality.

{mospagebreak title=Did Anyone Say MySQL?}

As mentioned briefly on the previous page, our tag board will take advantage of a MySQL database to store the information (tags). The MySQL table I will be using for this is below:

CREATE TABLE `tagboard` (
`tagId` int(11) NOT NULL auto_increment,
`tag_name` varchar(55) NOT NULL default ”,
`tag_url` varchar(255) NOT NULL default ”,
`tag_entry` text NOT NULL,
`tag_date` int(11) NOT NULL default ’0′,
PRIMARY KEY (`tagId`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;


You will need to add this table to a MySQL database; you can do this through the MySQL console or through a program like phpMyAdmin.

Our above table will store all our data (tags) indexed by a tagId, which is assigned to each tag by MySQL; this is commonly referred to as an “auto_increment” index. The reason I have included this column is because it is my preferred method of indexing, and it enables us to easily identify the tags if we were to create some admin functions (e.g. delete-tags.php?tagId=123).

The next field in our table is “tag_name” which will store the users name, this is a varchar as most Internet users generally possess an Internet-only secret identity and generally write their “name” using a multitude of strange characters. Which is fine by us. The only restriction we have is the number of characters in that name, in our case 55; we administer this simply by setting maxlength=”55” in our HTML input tag (e.g. <input type=”text” name=”__name” size=”20″ maxlength=”55″ class=”inputform”>).

Following this we have “tag_url” which will store the URL of the user, if applicable, meaning this is not a required field. It is a varchar also and is limited to 255 characters by maxlength=”255” again.

Now we are storing the body of our tag in the “tag_entry” field. This field is of the text type; this is limited to 400 characters by our HTML input tags once again.

The next field is where we are storing our date, “tag_date” which is of the int type, and 11 characters long. Some of you may be wondering why I have chosen to use “int” instead if “date” or “date-time”. The reason I have done this is because we are using the php Unix Epoch style of timestamp (time()) rather than the a MySQL timestamp.

{mospagebreak title=Our Tag Board Script}

Our tag board is made up of three functions, four defined constants, a switch statement and a nifty little comparison operator to help with those of you using error-reporting E_ALL.

So let’s have a look at the complete version of our script, and then we will go through and look at the script with greater detail. Our Tag Board Script can be found below:

<?
define(‘HOST’, ‘localhost’);
define(‘USER’, ‘mysql_username’);
define(‘PASS’, ‘mysql_pass);
define(‘DB’, ‘mysql_db’);

function doTags()
{
$conx = mysql_connect(HOST, USER, PASS) or die(“Unable to connect to MySQL Server”);
mysql_select_db(DB) or die(“Unable to select Database”);

$stSql = “SELECT tag_name, tag_url, tag_entry, tag_date FROM tagboard ORDER BY tag_date DESC”;
$arrResult = @mysql_query($stSql);
if (!$arrResult) { echo “Query Failed – Unable to Retrieve tags – “.mysql_error().” – Action Aborted”; exit(); }

if (mysql_num_rows($arrResult) >= 1)
{

while ($arrRow = mysql_fetch_assoc($arrResult))
{
?>
<table align=”center” width=”100%” bgcolor=”#FFFFFF”>
<tr>
<td><a href=”<? echo stripslashes($arrRow['tag_url']); ?>”><? echo stripslashes($arrRow['tag_name']); ?></a></td>
</tr>
<tr>
<td><? echo nl2br(stripslashes($arrRow['tag_entry'])); ?></td>
</tr>
<tr>
<td><span style=”font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #666666″> <? echo date(“F j, Y, g:i a”, $arrRow['tag_date']); ?></span></td>
</tr>
</table>
<?
}
} else {
?>
<p style=”font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF”>No entries to display</p>
<?
}
}

function doBoard()
{
?>
<html>
<head>
<title>DevShift.com Tag Board</title>
<style>
body {
font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF;
}
td {
font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; color: #666666;
}
.inputform {
BORDER-RIGHT: #efefef 1px solid; BORDER-TOP: #000000 1px solid; FONT: 11px Verdana; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #efefef 1px solid; BACKGROUND-COLOR: #efefef
}
.inputformsub {
BORDER-RIGHT: 1px outset; PADDING-RIGHT: 2px; BORDER-TOP: 1px outset; PADDING-LEFT: 2px; PADDING-BOTTOM: 2px; FONT: 11px Verdana; BORDER-LEFT: 1px outset; PADDING-TOP: 2px; BORDER-BOTTOM: 1px outset; BACKGROUND-COLOR: #dedede
}
</style>
</head>
<body>
<form action=”board.php?do=Insert” target=”tag_box” method=”POST”>
<table width=”150″ height=”250″ bgcolor=”#FFFFFF” style=”border: 1px solid #000000″>
<tr>
<td height=”150″><IFRAME name=”tag_box” height=”150″ width=”150″ src=”board.php?do=Tags” frameborder=”0″></IFRAME></td>
</tr>
<tr>
<td height=”100″ align=”center”>Name:<br><input type=”text” name=”__name” size=”20″ maxlength=”55″ class=”inputform”><br>URL:<br><input type=”text” name=”__url” size=”20″ maxlength=”255″ value=”http://” class=”inputform”><br>Your Entry:<br><input type=”text” name=”__entry” size=”20″ maxlength=”400″ class=”inputform”><br><input type=”submit” value=”Submit Entry” class=”inputformsub”></td>
</tr>
</table>
</form>
</body>
</html>
<?
}

function doInsert($__name, $__entry, $__url)
{
$conx = mysql_connect(HOST, USER, PASS) or die(“Unable to connect to MySQL Server”);
mysql_select_db(DB) or die(“Unable to select Database”);

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

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 {

$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().” – Action Aborted”; 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’”>
<?
}
}
}

$do = (isset($_GET['do']) ? $_GET['do'] : FALSE);

switch ($do) {
case “Board”:
doBoard();
break;
case “Tags”:
doTags();
break;
case “Insert”:
doInsert($_POST['__name'], $_POST['__entry'], $_POST['__url']);
break;
default:
doBoard();
}
?>


{mospagebreak title=What Makes Our Tag Board Tick?}

First off let’s look at our defined constants:

define(‘HOST’, ‘localhost’);
define(‘USER’, ‘mysql_username’);
define(‘PASS’, ‘mysql_pass);
define(‘DB’, ‘mysql_db’);

These constants represent the MySQL server connection variables and the MySQL database where our table can be found. Once they have been defined, they cannot be changed. For more information on these, please see http://www.php.net/manual/en/language.constants.php

Note: You need to change the HOST, USER, PASS, DB constants to match your MySQL host, username, password and database.

Next in the code are our three functions, which we will go into detail on separately. I wish to skip to the comparison operator and our switch statement. The code I am referring to is below:

$do = (isset($_GET['do']) ? $_GET['do'] : FALSE);

switch ($do) {
case “Board”:
doBoard();
break;
case “Tags”:
doTags();
break;
case “Insert”:
doInsert($_POST['__name'], $_POST['__entry'], $_POST['__url']);
break;
default:
doBoard();
}

The comparison operator is the first line of code in the snippet above. What this code is doing is checking whether the $do variable is set in the URL. This is achieved by first checking whether if the variable $do has been carried through the URL:

isset($_GET[‘do’])

Our ternary operator allows us to choose what we do with our variable depending on what our isset() function returns. To make it simple I will try to establish how the operator will work:

$do = (isset($_GET['do']) ? TRUE : FALSE);

So if our function returns true, we will execute the code in the “TRUE” part of our operator; if the isset function returns false, we will execute the code in the “FALSE” part of the function. In our case if the isset($_GET[‘do’] function returns true, we want to set the variable to the value contained in the URL, example:

Board.php?do=Insert

We would want $do to equal “Insert”. In the instance that no variables are passed through the URL, we want our $do variable to be set to false. We achieve this by using the following code:

$do = (isset($_GET['do']) ? $_GET['do'] : FALSE);

Switch Statement

Now let’s have a look at our switch statement. Below is a quote from www.php.net/switch, which I believe describes perfectly a switch statement.

“The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.”

Now our code:

switch ($do) {
case “Board”:
doBoard();
break;
case “Tags”:
doTags();
break;
case “Insert”:
doInsert($_POST['__name'], $_POST['__entry'], $_POST['__url']);
break;
default:
doBoard();
}

I won’t go into too much detail with switch statements; basically, if in our URL we have board.php?do=Tags, our script will run the dotages() function, or if there is no variable passed through the URL and our comparison operator sets $do to false, the default function will be run, in this case “doBoard()”. This is reasonably easy to grasp and getting too far into the inner workings of switch statements, which is out of the scope of this article, so we will move on to our functions.

{mospagebreak title=Our Tag Board Functions!}

We had to design our functions to achieve certain requirements. Those requirements are as follows:

• Retrieve tags
• Sort tags by date
• Report an error if one is encountered or if no tags exist in the database
• Display HTML formatted tags
• Create the HTML for the page with the inclusion of an inline frame (iframe)
• Include form for users to add a new tag
• Insert Posted data from form into database
• Add slashes to ensure database integrity
• Check for data in required fields (name, entry) and prompt user if null
• Insert into table and check whether successful

Now that we have identified our requirements, we can see a basic pattern of functions we are going to need. I have listed these below:

• doTags() – Retrieve, sort and report any errors encountered;
• doBoard() – Create the HTML page for the tag board including the iframe; and
• doInsert() – Insert posted data into database, adding slashes, checking for required fields and finally checking the success of the action.

So let’s have a look at our function in greater detail. First off, doTags():

function doTags()
{
$conx = mysql_connect(HOST, USER, PASS) or die(“Unable to connect to MySQL Server”);
mysql_select_db(DB) or die(“Unable to select Database”);

$stSql = “SELECT tag_name, tag_url, tag_entry, tag_date FROM tagboard ORDER BY tag_date DESC”;
$arrResult = @mysql_query($stSql);
if (!$arrResult) { echo “Query Failed – Unable to Retrieve tags – “.mysql_error().” – Action Aborted”; exit(); }

if (mysql_num_rows($arrResult) >= 1)
{

while ($arrRow = mysql_fetch_assoc($arrResult))
{
?>
<table align=”center” width=”100%” bgcolor=”#FFFFFF”>
<tr>
<td><a href=”<? echo stripslashes($arrRow['tag_url']); ?>”><? echo stripslashes($arrRow['tag_name']); ?></a></td>
</tr>
<tr>
<td><? echo nl2br(stripslashes($arrRow['tag_entry'])); ?></td>
</tr>
<tr>
<td><span style=”font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #666666″> <? echo date(“F j, Y, g:i a”, $arrRow['tag_date']); ?></span></td>
</tr>
</table>
<?
}
} else {
?>
<p style=”font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF”>No entries to display</p>
<?
}
}

A reasonably easy to navigate function - I will run you through how this function went about achieving its requirements.

{mospagebreak title=Retrieval and Sorting of Tags}

The retrieval and sorting of the tags is done with the following code:

/* Connect to Database */
$conx = mysql_connect(HOST, USER, PASS) or die(“Unable to connect to MySQL Server”);
mysql_select_db(DB) or die(“Unable to select Database”);

/* The MySQL Query */
$stSql = “SELECT tag_name, tag_url, tag_entry, tag_date FROM tagboard ORDER BY tag_date DESC”;

/* Submit Query to MySQL and check results */
$arrResult = @mysql_query($stSql);
if (!$arrResult) { echo “Query Failed – Unable to Retrieve tags – “.mysql_error().” – Action Aborted”; exit(); }

The first section we are connecting to our MySQL Database, taking advantage of our defined constants.

Secondly we set the SQL query we will use to retrieve the name of the user who posted the tag “tag_name”, the users website URL “tag_url”, the tag itself “tag_entry” and the date the tag was entered into the database “tag_date”. We then ask MySQL to retrieve the results from the “tagboard” table and ask MySQL to order the results by the date submitted “tag_date”.

And finally we submit the result to MySQL for processing. If an error is encountered it will be picked up, displayed to the user in a controlled fashion and then the script will be aborted.

Now we will look at the displaying of the tags in a formatted fashion:

/* Check wether MySQL returned 1 or more rows */
if (mysql_num_rows($arrResult) >= 1)
{
/* loop through rows – strip any slashes – display formatted tags */
while ($arrRow = mysql_fetch_assoc($arrResult))
{
?>
<table align=”center” width=”100%” bgcolor=”#FFFFFF”>
<tr>
<td><a href=”<? echo stripslashes($arrRow['tag_url']); ?>”><? echo stripslashes($arrRow['tag_name']); ?></a></td>
</tr>
<tr>
<td><? echo nl2br(stripslashes($arrRow['tag_entry'])); ?></td>
</tr>
<tr>
<td><span style=”font-family: verdana,arial,helvetica,sans-serif; font-size: 9px; color: #666666″> <? echo date(“F j, Y, g:i a”, $arrRow['tag_date']); ?></span></td>
</tr>
</table>
<?
}
/* If no rows were returned by the query, let the user know */
} else {
?>
<p style=”font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF”>No entries to display</p>
<?
}

{mospagebreak title=Results}

First off we have checked whether the MySQL query has returned 1 or more rows. We do this because the query may be executed successfully, but there may be no tags stored in the database, so we need to let the user know that no tags have been submitted yet.

If one or more results were returned by the database, we will look through them, displaying them one at a time using a while loop. Since we used the mysql_fetch_assoc() function, we will be accessing the specific fields by using the following syntax, $array_name[‘field_name’] . Therefore in our script we would use the following to display the field name minus the slashes echo stripslashes($arrow[‘tag_entry’]);

Note: The reason we remove the slashes is because in the doInsert() function we add slashes to the posted data to improve database integrity.

You may also notice a function in around the code to display to the tag entry which is nl2br(). This function will change n to <br /> which will improve overall formatting. More information is available at www.php.net/nl2br

In the instance where no rows were returned by the MySQL query, we would need to let the user know that there was nothing to display. We have achieved this with the “else” section of our if statement. If our MySQL query returned less than 1 row (e.g. no rows in the database), then we display a message to the user informing them of the situation.

{mospagebreak title=doBoard() Function}

The next function in our script is doBoard() which contains the HTML to format our tag board. Everything is reasonably basic and since knowledge of HTML is assumed, we won’t go into to much detail on this. The code used in the doBoard function can be found below:

function doBoard()
{
?>
<html>
<head>
<title>DevShift.com Tag Board</title>
<style>
body {
font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; background-color: #FFFFFF;
}
td {
font-family: verdana,arial,helvetica,sans-serif; font-size: 11px; color: #666666;
}
.inputform {
BORDER-RIGHT: #efefef 1px solid; BORDER-TOP: #000000 1px solid; FONT: 11px Verdana; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #efefef 1px solid; BACKGROUND-COLOR: #efefef
}
.inputformsub {
BORDER-RIGHT: 1px outset; PADDING-RIGHT: 2px; BORDER-TOP: 1px outset; PADDING-LEFT: 2px; PADDING-BOTTOM: 2px; FONT: 11px Verdana; BORDER-LEFT: 1px outset; PADDING-TOP: 2px; BORDER-BOTTOM: 1px outset; BACKGROUND-COLOR: #dedede
}
</style>
</head>
<body>
<form action=”board.php?do=Insert” target=”tag_box” method=”POST”>
<table width=”150″ height=”250″ bgcolor=”#FFFFFF” style=”border: 1px solid #000000″>
<tr>
<td height=”150″><IFRAME name=”tag_box” height=”150″ width=”150″ src=”board.php?do=Tags” frameborder=”0″></IFRAME></td>
</tr>
<tr>
<td height=”100″ align=”center”>Name:<br><input type=”text” name=”__name” size=”20″ maxlength=”55″ class=”inputform”><br>URL:<br><input type=”text” name=”__url” size=”20″ maxlength=”255″ value=”http://” class=”inputform”><br>Your Entry:<br><input type=”text” name=”__entry” size=”20″ maxlength=”400″ class=”inputform”><br><input type=”submit” value=”Submit Entry” class=”inputformsub”></td>
</tr>
</table>
</form>
</body>
</html>
<?
}

The main area of interest in this is the Inline Frame (iframe) where we open a new instance of our script in the Inline Frame. See Below:

<IFRAME name=”tag_box” height=”150″ width=”150″ src=”board.php?do=Tags” frameborder=”0″></IFRAME>

As you can see, we have named the frame “tag_box” and set the src (source) to board.php?do=Tags which is the function mentioned above. This as we know will display all the tags stored in our database.

Another area of this code we need to note is the form where users can submit their tags. More specifically we want to look at the following code:

<form action=”board.php?do=Insert” target=”tag_box” method=”POST”>

Which will tell the form to load the page “board.php?do=Tags” into our Inline Frame.

{mospagebreak title=doInsert() Function}

The last of our three functions is the doInsert() function, which is given the task of inserting a tag into the database, returning the result of the insert, and then refreshing only the page in the Inline Frame to display all tags, which will hopefully include the tag the user just submitted.

The code we used to achieve this is detailed below:

function doInsert($__name, $__entry, $__url)
{
$conx = mysql_connect(HOST, USER, PASS) or die(“Unable to connect to MySQL Server”);
mysql_select_db(DB) or die(“Unable to select Database”);

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

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 {
$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’”>
<?
}
}
}

As you can see in this first part of our function, we connect to our MySQL database and then select the database we will be using, which as I mentioned earlier in the article uses our defined constants.

{mospagebreak title=Ensuring Data Submitted Does Not Fail}

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.

{mospagebreak title=Inserting into the Tag Board}

The first and most significant is the SQL query we have used to insert the information into the database. That code is below:

$stSql = “INSERT INTO tagboard(tagId, tag_name, tag_url, tag_entry, tag_date) values(”, ‘$__name’, ‘$__url’, ‘$__entry’, “.time().”)”;

As you can see, we are Inserting Into our table “tagboard” into the following fields “(tagId, tag_name, tag_url, tag_entry, tag_date)”. We wish to place the following values, in order of how they appear, “values(”, ‘$__name’, ‘$__url’, ‘$__entry’, “.time().”)”;”. As you may have noticed we left the value which corresponds with tagId empty (‘’). We did this because as mentioned earlier the tagId field is auto increment, meaning if we leave the value empty, the next Id in line will be automatically assigned. We also used the php time() function for the tag_date field; this was explained in the “Did Anyone Say MySQL” section earlier in the article.

Next we check to see if our MySQL query was executed successfully. Since we covered this section in the doTags() function, we will move along to our if statement. This code is detailed 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’”>
<?
}

In this snippet we check to see if any MySQL rows were affected, which if the insert was successful, we simply refresh the Inline Frame to display the doTags() function and the user will see their tag. If the query failed will inform the user, then refresh the Inline Frame back to the doTags() function. We then close our elseif statement and close our function. Displayed below:

}
}

Conclusion

As you can see it is possible to create a Tag Board for your site and have it running out of one php script. We have achieved all our requirements with three functions.

If you followed this article correctly, you should have a tag board of your own ready to go on your site. You should also have the knowledge of how to Insert and retrieve data from a MySQL database.

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan