The legal tricks-Learn Your Self

Latest gadgets,softwares,hardware,reviews,programming and campuses, game cheats ext......

Creating a guestbook

In this tutorial I will show you how to create a simple guestbook where your visitors can leave messages which are stored in a MySQL database. It contains the basic functions and security settings.

After this short summary let’s see the steps how to realize it.

Step 1.
First of all try to think about what we need in this project. The most important is the main page where we can see the massages ordered by date. I personally like the newest on the top, but you can of course change it to the opposite order. So the main page contains the messages with the name of the writer and the datum when the message was submitted. The second most important functionality is to add new messages to the list. On the add page a visitor fill out a form with his/her name and the message and submit it. In this phase our script needs to do some input validations and security checks. As we want to store the messages in a database we need a connection to a MySQL server. As we need this connection in various files it makes sense to implement it in a separate file.

So at the end we need 3 files as follows:

1. index.php : This is the main page with the list of messages.
2. add.php: This script adds new messages to the list.
3. db.php: This script contains the database connection information and code.

Let’s start with the implementation in opposite order.

Step 2.
Before implementing our database connection script we need to design our database structure.

We want to store in this tutorial the following data:

* Visitor name
* Message
* Date and time when the message was added to the guestbook.
* ID to uniquely identify a message.

As result the sql code to realize this looks like this:

`id` int(11) NOT NULL auto_increment,
`name` varchar(100) default NULL,
`text` text,
`insertdate` datetime default NULL,
PRIMARY KEY (`id`)
)


Step 3.
So as we have the database table let’s try to connect to it. As this tutorial is not focusing on database issues so here I don’t make any long explanation. The db.php script just contains the important connection information than try to make a connection to the given MySQL server with the given user name and password. As next step it selects the right database. That’s all. All the other database queries will be implemented in the index.php and add.php scripts.

At the end our db.php script looks like
$serverhost = "localhost";
$serveruser = "username";
$serverpwd = "password";
$dbname = "test";

$connection = mysql_connect($serverhost,$serveruser,$serverpwd);
mysql_select_db($dbname,$connection);

?>

0 comments: