On this lesson you will learn in details how you can create your own guestbook application using PHP and MySQL.
Try to do not copy and paste the code, type all the script code that’s will help you understand more how the things are working.
Let’s start coding:
Create the mysql table # #The structure of the mysql guestbook table # CREATE TABLE guestbook_tbl ( id int NOT NULL auto_increment, date varchar(20) NOT NULL, username varchar (50) NOT NULL, email varchar(55) NOT NULL, message text NOT NULL, PRIMARY KEY (id) );
as you can see we have choose too many parameters to the mysql table, the id, date, username, email, and then the message with the id as the primary key.
Next will be the form that the user will use to submit his/her message, named as (form.html)
<html> <head> <title>Guestbook</title> </head> <body bgcolor="#ffffff"> <form method="post" action="add.php"> <input type="text" name="username"><br / > <input type="text" name="email"><br / > <textarea name="message" wrap="VIRTUAL" cols="40" rows="10"> </textarea><br / > <input type="submit" name="submit" value="Submit"> </form> </body> </html>
Here we have to code the page that will verify and add data to the mysql tabele; (add.php)
<?php
//----------------------------------------------------------------
//
// Adding data to the data base
//
//----------------------------------------------------------------
$mysql_host = 'localhost'; // Host name
$mysql_user = 'your login'; // Your login
$mysql_password = 'password'; // Your password
$mysql_base = 'data base name'; // enter the data base name
//----------------------------------------------------------------
// We need to check the fields the we want to be must...
//if they are empty or not
if( empty( $_POST['username']) || empty($_POST['message']) )
{ print'<a href="javascript:history.back();">Go back to Complet the form</a>'; }
else{ // The fields are ok, We Insert on the data base
//We set the date format
$date = date("Y-m-d H:i");
// We remove the special characters
$username = AddSlashes (htmlspecialchars($_POST['username']));
$email = AddSlashes (htmlspecialchars($_POST['email']));
$message = AddSlashes (htmlspecialchars($_POST['message']));
//Connect to the data base server
$db = mysql_connect($mysql_host,$mysql_user,$mysql_password) or die ("connexion error");
//we select the mysql data base
mysql_select_db($mysql_base,$db) or die ("base connexion error");
//Insert data into the data base
mysql_query("INSERT Into guestbook_tbl (id,date,username,email,messages) VALUES
('','$date','$username','$email','$message') ") or die ("request error ".mysql_error());
//We close the connexion
mysql_close();
//send the visitor to the messages page home.php
header('location: home.php');
}
//----------------- End of the SCRIPT --------------------------------
?>
Now the page that will display the submitted messages.
<?php
//----------------------------------------------------------------
//
// Page of displying messages
//
//----------------------------------------------------------------
$mysql_host = 'localhost'; // Host name
$mysql_user = 'your login'; // Your login
$mysql_password = 'password'; // Your password
$mysql_base = 'data base name'; // enter the data base name
//----------------------------------------------------------------
//Connect to the data base server
$db = mysql_connect($mysql_host,$mysql_user,$mysql_password) or die ("connexion error");
//we select the mysql data base
mysql_select_db($mysql_base,$db) or die ("connexion error base");
//The request to select the messages
$req = mysql_query("SELECT date,username,email,message from guestbook_tbl Order by date Desc ")
or die ("request error");
//we loop the result, and stock it an array with mysql_fetch_array()
while( $content = mysql_fetch_array ($req))
{
//we display the result
print 'Posted on : '.$content['date'].'<br / >';
print 'By : <a href="mailto:'.$content['email'].'">'.$content['username'].'</a><br / >';
print 'Message : <br />'.$content[message].'<br / >';
print '<hr />';
}
//We close the connexion
mysql_close();
//----------------- End of the SCRIPT --------------------------------
?>
And it’s the end of the script, try to do not copy and paste the code, by typing it with your fingers that’s will help you to catch more.
thnk u for the good tutorial