Writing a simple guestbook script using DBX

Personal Homepages often contain a small section called a guestbook - a place where people can come and write stuff about how they liked the website, or just to let the person know that they've visited the page. And mostly, these pages are the ones with very less traffic.

To create a backend that will handle this guestbook, we can write a simple PHP script that collects the guestbook message and inserts it into a database. To show all messages, we can simply list all the records of the database.

Observe how small this script is - it's just got two basic functions, one, to insert the data, and two, to display all existing records.

Now DBX is a great database system to be used for this - the database here is small, and since SQL is supported, the coding is minimal. So lets see how we can write this script for DBX.

First, we need to know what data to store. For simplicity we will take two records - the message written, and the name of the writer. To create the table in DBX we can simply execute this query in DBX Admin, or create the table using a text editor:

<?
include("/path/to/dbx/dbx.php");
$db=new dbx;
$db->dbx_query("CREATE TABLE guestbook(message,writer)");
?>

Note how we did not give any data types for the attributes - since DBX is written in PHP, the language handles all the typecasting, and thus datatypes are allowed, but not required.

Now we move on to our script. Our script has three parts - the code for the form, the code for inserting the form data, and the code for showing all saved data.

The guestbook form will have two fields and a submit button, and will look like this:

<form method="post" action="<?=$PHP_SELF?>">
Name:<br>
<input type="text" name="writer">
<br>
Message:<br>
<textarea name="message"></textarea>
<br>
<input type="submit" name="action" value="submit">
</form>
 
Name:

Message:

On clicking the submit button, the data is sent to the the script itself, so that the script can take the message data and insert it into the database using an insert query. For this, we will first create the DBX class, and then query the database.
<?
include("/path/to/dbx/dbx.php");
$db=new dbx;
if ($action=="submit")
$db->dbx_query("INSERT INTO guestbook values('$message','$writer')");
?>

Thus the data is stored. Now we need code to read all records in the database. This can be done using a select query. and then using a while loop to get all the data.
<?
$q=$db->dbx_query("SELECT * FROM guestbook");
while($record=$db->dbx_fetch_array($q))
print "
$record[MESSAGE] : $record[WRITER]";
?>

Done! So we've now got all the parts of a working guestbook - let's put them together to make a full fledged guestbook script - you can copy this code and make a script out of it, if you want to.


<?

//Include the DBX header and create DBX class...

include("/path/to/dbx/dbx.php");
$db=new dbx;

// Check if a message has been posted, then insert it.

if ($action=="submit")
$db->dbx_query("INSERT INTO guestbook values('$message','$writer')");

// Now select all the data, and print it one by one.

$q=$db->dbx_query("SELECT * FROM guestbook");
while($record=$db->dbx_fetch_array($q))
print "
$record[MESSAGE] : $record[WRITER]";
?>

<form method="post" action="<?=$PHP_SELF?>">

Name:<br>

<input type="text" name="writer">

<br>

Message:<br>

<textarea name="message"></textarea>

<br>

<input type="submit" name="action" value="submit">

</form>

So this was a very basic guestbook script - you can add many more fields to it, for example a timestamp to store the date, and then sort the records in reverse order. You can add a message on top displaying the number of messages(using a select count query).

There's a whole lot more that you can do with DBX - and it's a lot of fun too. Happy Scripting!!!

|