0% found this document useful (0 votes)
25 views

Lab 9 - PHP and MySQL

Lab 9 focuses on accessing a MySQL database using PHP Data Objects (PDO) and requires students to upload their website to a web server by November 22, 2024. The lab includes tasks such as gathering MySQL information, creating an 'Employee' table, and developing PHP scripts for account creation, login, and employee viewing. Students must submit their work as a compressed file containing specific PHP files on Brightspace for assessment, which is worth 3% of the total course mark.

Uploaded by

Kinas 04
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lab 9 - PHP and MySQL

Lab 9 focuses on accessing a MySQL database using PHP Data Objects (PDO) and requires students to upload their website to a web server by November 22, 2024. The lab includes tasks such as gathering MySQL information, creating an 'Employee' table, and developing PHP scripts for account creation, login, and employee viewing. Students must submit their work as a compressed file containing specific PHP files on Brightspace for assessment, which is worth 3% of the total course mark.

Uploaded by

Kinas 04
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 9 – Access MySQL database using

PDO (PHP Data Object)


Purpose
• Explore MySQL using PHP
• Upload your website to a Web server

Due Date
• This lab must be handed in:
Friday Nov 22, 2024 – before midnight

Assessment
• This Lab is worth 3% of your total course mark.

Assigned Readings
➢ Lecture Slides posted on Brightspace
• Module 4 -> Part 2
➢ The following chapters of Fundamentals of Web Development will be useful in
completing this exercise:
• Chapter 11

Lab Supplies
To complete this lab you will require the following lab supplies:

• Lecture Slides (Module 4 -> Part 2) and Code Examples (CRUD Example-EasyPHP.zip)
posted on Brightspace
• Textbook: Fundamentals of Web Development by Randy Connolly and Ricardo Hoar
• EasyPHP, or other WAMP server
• Eclipse, Notepad++ (or other text editor, or IDE)
Summary of Tasks
1. Gather MySQL Information
2. Understanding your database
3. Develop the logic to display your web application
4. View your webpage using a web browser
5. Submit Source Code of all PHP files on Brightspace

Task 1
Before we can get started using MySQL on the web hosting server we need a few pieces of
information.

• Host
o The host variable should contain the value “localhost”.
o Example: $host = “localhost”;
o NOTE: Localhost is a networking term meaning ‘this computer’

• Username
o The Username to access YOUR database MUST be as below:
$username = "cst8238";

• Password
o The password to access YOUR database MUST be as below:
$password = "cst@8238";

• Database Name
o The name of your database MUST be as below:
$database ="cst8238";

Task 2
To create your database, username, password and tables on the EasyPHP Web server, please
review the following document on Brightspace:

Course Content -> Module 4 – Dynamic Web Programming -> Part 2- PHP and MySQL ->
CRUD Example-EasyPHP.zip -> CRUD_PDO-EasyPHP -> Instruction_MySQL_EasyPHP.docx
Task 3
Now that we have a database we have to understand the tables inside it. Your database must
contain a table named Employee.

The ‘Employee’ table should have the following fields/columns:


• EmployeeId, INT, Primary Key, NOT NULL, AUTO INCREMENT
• FirstName, VARCHAR(50), NULL
• LastName, VARCHAR(50), NULL
• EmailAddress, VARCHAR(255), NULL
• TelephoneNumber, VARCHAR(20), NULL
• SocialInsuranceNumber, VARCHAR(11), NULL
• Password, VARCHAR(50), NULL

❖ NOTE
➢ EmployeeId is listed as ‘AUTO INCREMENT’ meaning that the database will
automatically populate this field.
➢ You MUST access the DB using PDO (PHP Data Object) as below:
$pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Implement the following Design Pattern to create a ‘Common Look and Feel’ to be used on
every page of your website.

Your web site will include the following PHP scripts:


• Header.php
• Footer.php
• Menu.php
• CreateAccount.php
• Login.php
• ViewAllEmployees.php

NOTE: CreateAccount.php, Login.php and ViewAllEmployees.php must include the Common


look and feel implemented in Header.php, Footer.php and Menu.php.
Header.php

Header.php must contain a script to display a Common Header that will appear on every page.
The header must display Program Name and Course Name

Footer.php

Footer.php must contain a script to display a Common Footer that will appear on every page.
The footer must contain Student Number, First Name, Last Name, and Email Address

Menu.php

Menu.php must contain a script to display a Common Menu to be shown on every page. The
menu must contain links to CreateAccount.php, Login.php and ViewAllEmployees.php.

CreateAccount.php

Create a form that will create employee accounts in the ‘Employee’ table of your database.

Details:
1. Use ‘input’ tags to accept the information listed in Task 3. Your form must populate all
the columns of the ‘Employee’ table.
2. After the information has been submitted to the database, save each of the values from
the form in the Session State.
3. Once the employee data is stored in the Session, automatically redirect the user to
‘ViewAllEmployees.php’

NOTE: EmployeeId is listed as ‘AUTO INCREMENT’ meaning that the database will
automatically populate this field.

NOTE: Session_StoreValues.php (posted on Brightspace) has an example of programmatically


sending an employee information to another page in your website.

Sample Screenshot for CreateAccount.php is as follows:


Login.php

The form Login.php allows the user to log into your application.

Details:
1. Create a form to accept the employees’s EmailAddress and Password as credentials to
your site. Use an SQL Query to determine if the person has an account.
2. If the user has an account, store ALL of their personal information in the Session State
and then redirect the user to ‘ViewAllEmployees.php’. Display an error if the user cannot
log into the system.

Sample Screenshot for Login.php is as follows:

ViewAllEmployees.php

This page pulls information from both the Session and the Database and displays the information
to the user.

Details:
1. If the employee tries to navigate to this page without having logged in – the employee
should be redirected to the login page.
2. If the employee has successfully logged into the application and is directed to the page,
then display the following information
a. Divide the content of the page into 2 sections (One on top of the other)
b. The top section of the page will display all details of the employee stored in the
Session State
c. The bottom section of the page will display a HTML table containing all rows
and columns of the ‘Employee’ table of your database. The HTML table must
also contain a header row that identifies the column name of the database table.
d. Provide H1 HTML headers so that both sections (Session State Data and
Database Data) are clearly marked
Sample Screenshot for ViewAllEmployees.php is as follows:

NOTE: To verify that an employee has successfully logged in, check for valid information in the
Session State. If the Session State does not contain valid information redirect to the login page,
otherwise, the user has logged in successfully.

NOTE: All the sample screenshots must include common header, footer and menu files.

Task 4
Create Lab 9 submission folder ‘Lab9’ and copy CreateAccount.php, Login.php,
ViewAllEmployees.php, Header.php, Footer.php, Menu.php and any other required files (e.g.
css file) into this folder.

Task 5
Create a compressed file (Lab9.zip) which will contain the following PHP files:
• Header.php, Footer.php, Menu.php
• CreateAccount.php
• Login.php
• ViewAllEmployees.php
• any other required files (e.g. css file)

(N.B. Please keep in mind that ONLY .zip file is accepted as the format of the compressed file.)

To hand in your lab go to Brightspace and navigate to Content → Labs and click on ‘Lab 9 – PHP
and MySQL’ link.

Upload the compressed file (Lab9.zip) on Brightspace.

Finally, click the ‘Submit’ button to send the lab to your professor.

You might also like