Lesson 3: Creating a New Wisher 
Contributed by Barbara Mityashina
June 27, 2008
Contents
Application Source Code from the Previous Lesson
Click here to download the source code that reflects the project state after the previous lesson is completed.
Lesson Scope
In this lesson you will expand the application with the Create a New Wisher functionality.
The implementation will affect the index.php file and two new files will be created named createNewWisher.php and editWishList.php.
The Create a New Wisher use case consists of two subsequent steps:
Adding a Link to Start Creating a New Wisher
Enter the following code block into the body of index.php:
Still don't have a wish list?! <a href="createNewWisher.php">Create now</a>
Where:
- Still don't have a wish list?! is the text that will be displayed on the page next to the link.
- <a href="createNewWisher.php"></a> is the code that implements a link that opens the createNewWisher.php page.
- Create now is the text that will be displayed as a link.
Functionality of createNewWisher.php
Implementing the functionality of this page consists of three steps:
HTML Form for Inputting the Data of a New Wisher
- Create and open the createNewWisher.php and editWishList.php files as described above .
- Enter the following code block:
<html><body>
Welcome!<br>
<form action="createNewWisher.php" method="POST">
Your name: <input type="text" name="user"/><br/>
Password: <input type="password" name="password"/><br/>
Please confirm your password: <input type="password" name="password2"/><br/>
<input type="submit" value="Register"/>
</form>
</body></html>
Note: The type password is a special type of a text field where characters are replaced with asterisks.
The code presents an
HTML form that enables entering the name and password of the new wisher in the text fields. When the user clicks the "Register" button, the entered data is transferred for validation to the same page, createNewUser.php.
Validating Data before Adding It to the Database
Enter the <?php ?> tags above the HTML code with the input form. The position of the PHP code block is important to enable correct functioning of the redirection statement. Enter the code blocks described below in this section.
Initializing variables
$userNameIsUnique = true;
$passwordIsValid = true;
$userIsEmpty = false;
$passwordIsEmpty = false;
$password2IsEmpty = false;
The code initializes the boolean variables that will be used in validation below.
Checking where the createNewWisher.php page was requested from
if ($_SERVER["REQUEST_METHOD"] == "POST")
The code checks that the page was requested from itself via the POST method.
If not, the further validations are not performed and the page is shown with empty fields as described above
Checking whether the user has filled in the wisher's name
{
if ($_POST["user"]=="")
$userIsEmpty = true;
The code checks whether any data is transferred in the text field "user". If the text field is empty, the value of $userIsEmpty is changed to true.
Otherwise passes to establishing a database connection.
Establishing a connection to the database
$con = mysql_connect("localhost", "phpuser", "!phpuser");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
The code attempts to establish a connection to the database and displays an error message if the attempt fails.
Checking whether a wisher with the specified name already exists
mysql_select_db("wishlist", $con);
$wisher = mysql_query("SELECT ID FROM wishers WHERE Name='".$_POST["user"]."'");
$wisherID = mysql_result($wisher, 0);
if ($wisherID) {
$userNameIsUnique = false;
}
mysql_close($con);
If the connection is established successfully, the code attempts
to retrieve the ID of the new wisher whose name is transferred through the "user" name. If such ID exists (the wisher with the specified name already exists), the value of userNameIsUnique is changed to false.
The connection is closed unconditionally.
Checking whether the user entered and confirmed a password correctly
if ($_POST["password"]=="")
$passwordIsEmpty = true;
if ($_POST["password2"]=="")
$password2IsEmpty = true;
if ($_POST["password"]!=$_POST["password2"])
$passwordIsValid = false;
}
The code checks that the Password ("password") and Confirm Password ('password2) fields are not empty in the form and that they are identical. Otherwise the values of the corresponding boolean variables are changed accordingly.
Adding the New Wisher to the Database
Continue the code block started in section Validating Data before Adding It to the Database.
if (!$userIsEmpty && $userNameIsUnique && !$passwordIsEmpty && !$password2IsEmpty && $passwordIsValid) {
$con = mysql_connect("localhost", "phpuser", "!phpuser");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("wishlist", $con);
mysql_query("INSERT wishers (Name, Password) VALUES ('".$_POST["user"]."', '".$_POST["password"]."')");
header('Location: editWishList.php' );
exit;
}
}
The code checks that the name of the wisher is specified uniquely and that the password is entered and confirmed validly. If the conditions are met, the code establishes a connection to the databases and adds a new record with the specified name to the table wishers. The ID is assigned to the wisher automatically because you specified "auto-increment" for the ID field during the table creation. After that the code redirects the application to the page editWishList.php.
Displaying Error Messages in the Input Form
Now implement displaying the error messages when the entered data is invalid. The implementation is based on the validations and changes to the values of the boolean variables described in section Validating Data before Adding It to the Database.
- Enter the following PHP code block inside the HTML input form, below the wisher's name input:
<?php
if ($userIsEmpty) {
echo ("Enter your name, please!");
echo ("<br/>");
}
if (!$userNameIsUnique) {
echo ("The person already exists. Please check the spelling and try again");
echo ("<br/");
}
?>
- Enter the following
PHP code block inside the HTML input form below the code for the password input:
<?php
if ($passwordIsEmpty) {
echo ("Enter the password, please!");
echo ("<br/>");
}
?>
- Enter the following PHP code blocks inside the HTML input form below the code for password confirmation:
<?php
if ($password2IsEmpty) {
echo ("Confirm your password, please");
echo ("<br/>");
}
if (!$password2IsEmpty && !$passwordIsValid) {
echo ("The passwords do not match!");
echo ("<br/");
}
?>
Testing the Create New Wisher Functionality
- Run the application. The index page opens.

- On the index.page, click the link next to the text Still don't have a wish list? The following form opens:

- Leave only the Your name field empty and click Register. An error message displays.

- Enter the name of a registered wisher, for example, Tom in the Your name field, fill in the other fields correctly, and click Register. An error message displays.
- Fill in the Password and Please confirm your password fields with different values and click Register. An error message displays.
- Enter Bob in the Your name field, specify the same password in both password fields and click Register. The page that opens is empty but the redirection passed correctly as the URL ends with editWishList.php:

- To check that the data is stored in the database, navigate to wishers on the Services window below the wislist1 node and from the context menu choose View Data

Application Source Code after the Current Lesson Is Completed
Click here to download the source code that reflects the project state after the lesson is completed
<< Previous lesson
Next lesson >>
Back to the Tutorial main page
Back to the PHP Learning Trail