CS 314 –
Web Application Development Lab Script
Date: / /
Department of Forensic Computing and Cyber Security
College of Computer Science and Information Technology
1. Objectives
This lab is designed to achieve the following goals:
Students shall create a MySQL database using phpMyAdmin and use PHP language to
talk to the MySQL database created. A login page shall be created that allows new users
to register information and existing users to login to a site.
2. Introduction
The topic of Week 5 provides an introduction to PHP+MYSQL, including PHP syntax,
PHP Forms, and PHP MySQL Database where you will be able to connect to your
database through PHP. Small examples are used to illustrate many of the PHP elements
that are discussed in this chapter. A discussion of the parts of PHP+MySQL that are now
widely supported is included...
3. Equipment / Apparatus / Materials / Environment / Tools and Techniques
The labs for CS-314 provide time to do computer science under the direction of an
instructor who will be serving as a facilitator during the lab session. This means that the
instructor will not solve your problems or show you how to complete the activities. The
instructor may provide helpful hints, ask additional questions, ask you to explain a
solution, ask how you obtained a solution, or facilitate discussions about observations,
experiments, results, discoveries, and other topics that arise.
Some lab activities will ask you to develop solutions to problems, program solutions in
Web Application Development, and test the programs. Other activities will involve using
programs we give you to illustrate important concepts in Computer Science. You will be
required to make observations, form and test hypotheses, and use the results of your
testing to change aspects of your programs.
4. Procedures / Getting Started / PHP + MySQL
PHP Syntax Example (PHP Syntax, Comments, Variables, Echo/print, Data types, Strings, Numbers,
Constants, Operators, if...Else...Elseif, Switch, Loops, Functions, Arrays. Superglobals.)
PHP Forms - (PHP Form Handling, PHP Form Validation, PHP Form Required, PHP Form URL/E-
mail, PHP Form Complete.)
PHP MySQL Database - (MySQL Connect, MySQL Create DB, MySQL Create Table, MySQL
Insert Data, MySQL Prepared, MySQL Select Data, MySQL Where, MySQL Order By, MySQL Delete
Data, MySQL Update Data, MySQL Limit Data.)
CS 314 –
Web Application Development Lab Script
Date: / /
MySQL Database
To connect to database through PHP execute the following code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username,
$password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
The output of the above code should be: “Connected successfully”
Now go to your localhost and type phpMyAdmin
http://localhost/phpmyadmin/
Create a Database
CS 314 –
Web Application Development Lab Script
Date: / /
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
Output
Create Table through PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE StudentSchedule (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
CS 314 –
Web Application Development Lab Script
Date: / /
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table Schedule created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
Output
MySQL Insert Data
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
CS 314 –
Web Application Development Lab Script
Date: / /
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO StudentSchedule (firstname, lastname,
email)
VALUES ('John', 'Smith', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Output
MySQL Insert Multiple
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO StudentSchedule (firstname, lastname, email)
VALUES ('John', 'Smith', 'john@example.com');";
$sql .= "INSERT INTO StudentSchedule (firstname, lastname, email)
VALUES ('Novak', 'Moe', 'Novak@example.com');";
CS 314 –
Web Application Development Lab Script
Date: / /
$sql .= "INSERT INTO StudentSchedule (firstname, lastname, email)
VALUES ('Rami', 'Dooley', 'Rami@example.com')";
if ($conn->multi_query($sql) === TRUE) {
echo "New records created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Output
MySQL Prepared
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt = $conn->prepare("INSERT INTO StudentSchedule (firstname, lastname, email)
VALUES (?, ?, ?)");
$stmt->bind_param("sss", $firstname, $lastname, $email);
// set parameters and execute
$firstname = "John";
$lastname = "Smith";
$email = "john@example.com";
$stmt->execute();
CS 314 –
Web Application Development Lab Script
Date: / /
$firstname = "Novak";
$lastname = "Moe";
$email = "Novak@example.com";
$stmt->execute();
$firstname = "Rami";
$lastname = "Dooley";
$email = "julie@example.com";
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
?>
Output
MySQL Select Data
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM StudentSchedule";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
CS 314 –
Web Application Development Lab Script
Date: / /
echo "0 results";
}
$conn->close();
?>
Output
MySQL Where
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM StudentSchedule WHERE lastname='Moe'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Output
MySQL Order By
CS 314 –
Web Application Development Lab Script
Date: / /
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM StudentSchedule ORDER BY lastname";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
Output / Results
MySQL Delete Data
<?php
CS 314 –
Web Application Development Lab Script
Date: / /
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to delete a record
$sql = "DELETE FROM StudentSchedule WHERE id=5";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
Output
MySQL Update Data
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE StudentSchedule SET lastname='Doe' WHERE id=2";
if ($conn->query($sql) === TRUE) {
CS 314 –
Web Application Development Lab Script
Date: / /
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
Output
Your Task:
The goal of this task is to demonstrate knowledge of php + mySQL through
1. Create a new folder within webserver root i.e. htdocs. The name of the folder
should be your student number_t5.
2. Create PHP User Registration Form (Sign up) with MySQL Database:
a. PHP code. Example (index.php, signup.php, login.php, etc.….)
b. CSS. Style.css (stylesheet)
c. Database table: (employees, students, memberships, etc.…)
3. Your code should include:
a. Getting user information via a HTML form.
b. Validating user submitted information on form submit.
c. Database handling to save registered user to the database after
validation.
Assessment
1. Each student will show all the above parts running as demo to the Lab Instructor before
leaving the lab. Total marks for the lab is as follows
Marks (demo +
Part (demo + report)
report)
1 30
2 10
3 60
Total 100
CS 314 –
Web Application Development Lab Script
Date: / /
2. Students will prepare a report in which they will submit the snapshots taken while they
worked on each part. They will explain the figures to make sure that they understood
what they did.
3. The deadline to submit the report is xx.xx.xxxx at 11:59 pm at xxxxxxxx@upm.edu.sa
Note: Any submission crossing the deadline will be penalized by 10% reduction per day.