Now we will go for adding data to the mysql database, but before that we will check the entry with our form validation techniques. We will set one flag and message then with each failure of validation we will add the message. If the form validation is ok then we will add data to the database by using mysql insert command, or we will display the error message to the visitor. We will also take care of posted data if register_global is set to OFF. Here is the code.
// to take care if register_global is offf
$email=$_POST['email'];
$name=$_POST['name'];
$country=$_POST['country'];
$dtl=$_POST['dtl'];
$status = "OK"; // setting the flag for form validation
$msg=""; // error message string is blank
// now let us check email address
if (!stristr($email,"@") OR !stristr($email,".")) {
$msg="<center>Your email address is not
correct</center><BR>";
$status="NOT OK";
}
// Now let us check if name is entered or not
if(strlen($name) < 2 ){ // if name is less than two char length
$msg .="<center>Please enter your
name</center><BR>";
$status="NOT OK";
}
if($status<>"OK"){ // if form validation is not passed
echo "<BR><BR>";
echo $msg. "<br><center><input type='button'
value='Retry' onClick='history.go(-1)'></center><br><br><br>";
}else{
$tm=time(); // reading the time of entry
// adding data to mysql database
$rt=mysql_query("insert into guest_book(name,email,country,tm,dtl)
values('$name','$email','$country','$tm','$dtl')");
echo mysql_error();
echo "<center><font face='Verdana' size='2'>Entry added,
Thank YOu. <a href=view_entry.php>Click here to view entries</a>.
</font></center><br><br><br>";
}
Now let us design the page to show the guestbook entries to the visitors. Here the order of display is the most recent entry is at the top. Changing the sql order by clause we can change this. Here we have used the sql fetch array command to collect the records and display them. Here is the code.
$query=mysql_query("select * from guest_book order by tm desc");