SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

Data Validation of PHP GuestBook script & storing in MySQL database. Part II



<< PHP guestbook part I

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");

echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";

while($nt=mysql_fetch_array($query)){ $dt=date("m/d/y",$nt[tm]); // formating the date
$dtl=nl2br($nt[dtl]); // this will change the line breaks to html line breaks
echo "<tr><td><font face='Verdana' size='2'><b>Name</b></font></td><td><font face='Verdana' size='2'>$nt[name]</font></td>";
echo "<td><font face='Verdana' size='2'><b>Email</b></font></td><td><font face='Verdana' size='2'>$nt[email]</font></td></tr>";
echo "<tr bgcolor=#f1f1f1><td><font face='Verdana' size='2'><b>Country</b></font></td><td><font face='Verdana' size='2'>$nt[country]</font></td>";
echo "<td><font face='Verdana' size='2'><b>Date:</b></font></td><td><font face='Verdana' size='2'>$dt</font></td></tr>";
echo "<tr><td colspan=4><font face='Verdana' size='2'>$dtl</font></td></tr>";
echo "<tr><td colspan=4><hr></td></tr>";
}

echo "</table>";

echo "<center><font face='Verdana' size='2'><a href='entry.html'>Add entry</a></center><br><br><br>";


To this scirpt many features like
php paging, admin management of all entries and some othere features can be added.

Download the ZIP file here  guest_book.zip

Discuss this tutorial at forum


 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.