PHP/MySQL : Creating a guest book
Olivier Roble
Checking Input Validity
Our form data will be sent to the
ajoutimp.php page that will connect to our database and run a SQL query to insert them. But before doing this, we must make sure that
the expected data are valid. The checking process is simple. The goal is to know if the mandatory fields (nom, e-mail and
impression) have been correctly filled in. If not, we display a warning message.
1 : <?php
2 : $continu = 1;
3 : if ($nom == "")
4 : {
5 : print ("Your name is needed ! <br>");
$continu = 0 ;
6 : }
7 : else
8 : {
9 : print ("Thank you for giving your name");
10: }
11: ?>
The various pieces of information collected through our form are accessible through different variables. These have the same
name as the form controls used to collect data. For instance, the value of the
nom text field is stored in a variable called
$nom.
We just need to test the presence of a value in the variables. For this, we must first compare the variable
$nom with an empty string,
"" (line 3), using the equality operator
==.
If the result of the comparison is true (the variable is indeed empty), we display an error message and assign
0 to the
$continu variable (line 5). We use this variable as a flag for the script operation. If its value is
1, we can continue with the script execution. If it is
0, the insertion of the data into the database is cancelled.
We follow the same method to check the presence of an e-mail address.