Create two new PHP web pages in your
project's Source Files.
- createNewWisher.php
- editWishList.php
In editWishList.php, include the content "Hi!" to the HTML body and generally abandon it with its default content. You will change this record in later lessons, yet you require it to exist now in light of the fact that createNewWisher.php references it. For the rest of this lesson, you alter createNewWisher.php.
Type or paste the following HTML
block into createNewWisher.php, beneath the PHP block:
<html>
<head>
<meta
http-equiv="content-type" content="text/html;
charset=UTF-8">
<title></title>
</head>
<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>
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 secret key sort is an exceptional kind of a content field where characters are supplanted with reference bullets. The code shows a HTML structure for a client to enter the name and secret key of the new wisher in the content fields. At the point when the client taps the "Register" catch, the entered information is exchanged for approval to the same page, createNewWisher.php. Note: You can overlook notices from the HTML validator.
Approving Data and Adding It to the Database
In this area you add PHP code to createNewWisher.php. Add this code to the PHP hinder at the highest point of the record. The PHP square should be most importantly HTML code, unfilled lines, or whitespace. The position of the PHP code square is imperative to empower right working of the redirection proclamation. Inside the PHP square, sort or glue the code pieces depicted underneath in this area, in the request they are composed.
Add the accompanying code to approve information:
Instate variables. The main variables pass database certifications and the others are the variables that will be utilized as a part of the PHP operations.
2. /** database connection credentials
*/
$dbHost="localhost"; //on MySql
$dbHost="localhost"; //on MySql
3. $dbXeHost="localhost/XE";
$dbUsername="phpuser";
$dbPassword="phpuserpw";
$dbUsername="phpuser";
$dbPassword="phpuserpw";
4.
5. /** other variables */
6. $userNameIsUnique = true;
7. $passwordIsValid = true;
8. $userIsEmpty = false;
9. $passwordIsEmpty = false;
10.$password2IsEmpty
= false;
11.
13./**
Check that the page was requested from itself via the POST method. */
14.if
($_SERVER["REQUEST_METHOD"] == "POST") {
15.
}
- Within the curly braces of the if clause, add another if clause that checks whether the user has filled in the wisher's name. If the text field "user" is empty, the value of $userIsEmpty is changed to true.
17./**
Check that the page was requested from itself via the POST method. */
18.if
($_SERVER["REQUEST_METHOD"] == "POST") {
19.
20./**
Check whether the user has filled in the wisher's name in the text field
"user" */
21. if ($_POST["user"]=="")
{
22. $userIsEmpty = true;
23. }
}
- Add code that establishes a database connection. If the connection cannot be established, the MySQL or Oracle OCI8 error is sent to the output.
For
the MySQL database:
/**
Check that the page was requested from itself via the POST method. */
if
($_SERVER["REQUEST_METHOD"] == "POST") {
/**
Check whether the user has filled in the wisher's name in the text field
"user" */
if ($_POST["user"]=="")
{
$userIsEmpty = true;
}
/** Create database connection */
$con = mysqli_connect($dbHost,
$dbUsername, $dbPassword);
if (!$con) {
exit('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
//set the default client character set
mysqli_set_charset($con, 'utf-8');
if (!$con) {
exit('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
//set the default client character set
mysqli_set_charset($con, 'utf-8');
}
For
the Oracle database:
/**
Check that the page was requested from itself via the POST method. */
if
($_SERVER['REQUEST_METHOD'] == "POST") {
/**
Check whether the user has filled in the wisher's name in the text field
"user" */
if ($_POST['user'] == "") {
$userIsEmpty = true;
}
/** Create database connection */
$con = oci_connect($dbUsername,
$dbPassword, $dbXeHost, "AL32UTF8");
if (!$con) {
$m = oci_error();
exit('Connect Error' . $m['message']);
}
}
- Add code that checks whether a user whose name matches the "user" field already exists. The code does this by trying to find a wisher ID number for a name matching the name in the "user" field. If such an ID number exists, the value of $userNameIsUnique is changed to "false."
For
the MySQL database:
/**
Check that the page was requested from itself via the POST method. */
if
($_SERVER["REQUEST_METHOD"] == "POST") {
/**
Check whether the user has filled in the wisher's name in the text field
"user" */
if
($_POST["user"]=="") {
$userIsEmpty = true;
}
/** Create database connection */
$con = mysqli_connect($dbHost, $dbUsername, $dbPassword);
if (!$con) {
exit('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
/**set the default client character set */
mysqli_set_charset($con, 'utf-8');
$con = mysqli_connect($dbHost, $dbUsername, $dbPassword);
if (!$con) {
exit('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
/**set the default client character set */
mysqli_set_charset($con, 'utf-8');
/** Check whether a user whose name
matches the "user" field already exists */
mysqli_select_db($con,
"wishlist");
$user = mysqli_real_escape_string($con,
$_POST["user"]);
$wisher = mysqli_query($con, "SELECT id FROM wishers WHERE name='".$user."'");
$wisherIDnum=mysqli_num_rows($wisher);
if ($wisherIDnum) {
$userNameIsUnique = false;
}
$wisher = mysqli_query($con, "SELECT id FROM wishers WHERE name='".$user."'");
$wisherIDnum=mysqli_num_rows($wisher);
if ($wisherIDnum) {
$userNameIsUnique = false;
}
}
For
the Oracle database:
/**
Check that the page was requested from itself via the POST method. */
if
($_SERVER['REQUEST_METHOD'] == "POST") {
/**
Check whether the user has filled in the wisher's name in the text field
"user" */
if ($_POST['user'] == "") {
$userIsEmpty = true;
}
/** Create database connection */
$con = oci_connect($dbUsername,
$dbPassword, $dbXeHost, "AL32UTF8");
if (!$con) {
$m = oci_error();
exit('Connection Error ' .
$m['message']);
}
/** Check whether a user whose name
matches the "user" field already exists */
$query = "SELECT id FROM wishers
WHERE name = :user_bv";
$stid = oci_parse($con, $query);
$user = $_POST['user'];
$wisherID = null;
oci_bind_by_name($stid, ':user_bv', $user);
oci_execute($stid);
//
Each user name should be unique. Check if the submitted user already exists.
$row = oci_fetch_array($stid, OCI_ASSOC);
if ($row){
$userNameIsUnique = false;
}
}
- After the code that checks if the user is unique, add a series of if clauses that check whether the user entered and confirmed a password correctly. 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.
27.if
($_POST["password"]=="") {
$passwordIsEmpty = true;
$passwordIsEmpty = true;
28.}
if ($_POST["password2"]=="") {
$password2IsEmpty = true;
if ($_POST["password2"]=="") {
$password2IsEmpty = true;
29.}
if ($_POST["password"]!=$_POST["password2"]) {
$passwordIsValid = false;
if ($_POST["password"]!=$_POST["password2"]) {
$passwordIsValid = false;
}
- Complete the if ($_SERVER['REQUEST_METHOD'] == "POST") clause by adding code that inserts a new entry into the "wishers" database. 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 takes the "user" and "password" values from the HTML form and inserts them into the Name and Password columns, respectively, of a new row in the wishers database. After creating the row, the code closes the database connection and redirects the application to the page editWishList.php.
For
the MySQL database:
/**
Check that the page was requested from itself via the POST method. */
if
($_SERVER['REQUEST_METHOD'] == "POST") {
/** Check whether the user has filled in
the wisher's name in the text field "user" */
if ($_POST['user'] == "") {
$userIsEmpty = true;
}
/** Create database connection */
$con = mysqli_connect($dbHost, $dbUsername,
$dbPassword);
if (!$con) {
exit('Connect Error (' .
mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
//set the default client character set
mysqli_set_charset($con, 'utf-8');
/** Check whether a user whose name matches
the "user" field already exists */
mysqli_select_db($con,
"wishlist");
$user = mysqli_real_escape_string($con,
$_POST['user']);
$wisher = mysqli_query($con, "SELECT
id FROM wishers WHERE name='".$user."'");
$wisherIDnum=mysqli_num_rows($wisher);
if ($wisherIDnum) {
$userNameIsUnique = false;
}
/** Check whether a password was entered
and confirmed correctly */
if ($_POST['password'] == "") {
$passwordIsEmpty = true;
}
if ($_POST['password2'] == "") {
$password2IsEmpty = true;
}
if ($_POST['password'] !=
$_POST['password2']) {
$passwordIsValid = false;
}
/** Check whether the boolean values show that
the input data was validated successfully.
* If the data was validated successfully,
add it as a new entry in the "wishers" database.
* After adding the new entry, close the
connection and redirect the application to editWishList.php.
*/
if (!$userIsEmpty &&
$userNameIsUnique && !$passwordIsEmpty && !$password2IsEmpty
&& $passwordIsValid) {
$password =
mysqli_real_escape_string($con, $_POST['password']);
mysqli_select_db($con,
"wishlist");
mysqli_query($con, "INSERT wishers
(name, password) VALUES ('" . $user . "', '" . $password .
"')");
mysqli_free_result($wisher);
mysqli_close($con);
header('Location: editWishList.php');
exit;
}
}
For
the Oracle database:
/**
Check that the page was requested from itself via the POST method. */
if
($_SERVER['REQUEST_METHOD'] == "POST") {
/**
Check whether the user has filled in the wisher's name in the text field
"user" */
if ($_POST['user'] == "")
$userIsEmpty = true;
/** Create database connection */
$con = oci_connect($dbUsername,
$dbPassword, $dbXeHost, "AL32UTF8");
if (!$con) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
/** Check whether a user whose name matches
the "user" field already exists */
$query = "select ID from wishers where
name = :user_bv";
$stid = oci_parse($con, $query);
$user = $_POST['user'];
$wisherID = null;
oci_bind_by_name($stid, ':user_bv', $user);
oci_execute($stid);
/**Each
user name should be unique. Check if the submitted user already exists. */
$row = oci_fetch_array($stid, OCI_ASSOC);
if ($row) {
$wisherID = $row['ID'];
}
if ($wisherID != null) {
$userNameIsUnique = false;
}
//Check for the existence and validity of
the password
if ($_POST['password'] == "") {
$passwordIsEmpty = true;
}
if ($_POST['password2'] == "") {
$password2IsEmpty = true;
}
if ($_POST['password'] !=
$_POST['password2']) {
$passwordIsValid = false;
}
/** Check whether the boolean values show
that the input data was validated successfully.
* If the data was validated successfully,
add it as a new entry in the "wishers" database.
* After adding the new entry, close the
connection and redirect the application to editWishList.php.
*/
if (!$userIsEmpty &&
$userNameIsUnique && !$passwordIsEmpty && !$password2IsEmpty
&& $passwordIsValid) {
$query = "INSERT INTO wishers
(name, password) VALUES (:user_bv, :pwd_bv)";
$stid = oci_parse($con, $query);
$pwd = $_POST['password'];
oci_bind_by_name($stid, ':user_bv',
$user);
oci_bind_by_name($stid, ':pwd_bv',
$pwd);
oci_execute($stid);
oci_free_statement($stid);
oci_close($con);
header('Location: editWishList.php');
exit;
}
}
Presently you actualize the presentation of mistake messages when the entered information is invalid. The execution depends on the acceptances and changes to the estimations of the boolean variables depicted in Validating Data and Adding It to the Database.
- Enter the following PHP code block inside the HTML input form, below the wisher's name input:
2. Welcome!<br>
<form action="createNewWisher.php" method="POST">
Your name: <input type="text" name="user"/><br/>
<form action="createNewWisher.php" method="POST">
Your name: <input type="text" name="user"/><br/>
3.
4.
5. <?php
6. if ($userIsEmpty) {
7. echo ("Enter your name,
please!");
8. echo ("<br/>");
9. }
10. if (!$userNameIsUnique) {
11. echo ("The person already exists.
Please check the spelling and try again");
12. echo ("<br/>");
13. }
?>
- Enter the following PHP code block inside the HTML input form below the code for the password input:
15.Password:
<input type="password" name="password"/><br/>
16.
17.<?php
18.
if ($passwordIsEmpty) {
19. echo ("Enter the password,
please!");
20. echo ("<br/>");
21.
}
?>
- Enter the following PHP code blocks inside the HTML input form below the code for password confirmation:
23.Please
confirm your password: <input type="password"
name="password2"/><br/>
24.
25.
26.
27.<?php
28.
if ($password2IsEmpty) {
29. echo ("Confirm your password,
please");
30. echo ("<br/>");
31.
}
32.
if (!$password2IsEmpty && !$passwordIsValid) {
33. echo
("The passwords do not match!");
34. echo ("<br/>");
35.
}
?>
No comments :
Post a Comment