Professional Documents
Culture Documents
Class: BSCS-10AB
Introduction:
Students have learned the advanced concepts of PHP during lecture including CRUD operations
on a relational database management system, file handling, working with PHP super global
arrays such as $_SESSION, $_COOKIE etc. In this lab the students will get practical multipage
web development knowledge using the advanced PHP concepts.
Lab Objectives:
The objective of this lab is helping students to familiarize themselves with the usage of PHP to
create multipage dynamic websites by communicating with relational databases, working with
file upload/retrieval and display over a page, and using super global arrays such as $_SESSION
and $_COOKIE.
Tools:
Notepad, DreamWeaver, browser.
Helping Material:
Lecture slides.
W3Schools: https://www.w3schools.com/php/
PHP: www.php.net
Lab Task
Notes:
1. In the previous lab, you have already worked with the ‘classicmodels’ database. You
have to add a new column in the ‘employees’ table to store the path of the profile
picture of each employee. The name of the column should be ‘picPath’ and it should be
of type varchar (255).
2. Using phpMyAdmin, make sure that the ‘employees’ table is updated successfully and
you can see the new column.
Task 1
Extend the script you have created in the previous lab so that the employee’s picture is also
present in the table (in first column). To retrieve the picture, you have to find the path of the
picture from the ‘picPath’ column and then use this path in the ‘src’ attribute of the <img> tag.
100 Market
Street, Suite Diane
VP
Jeff Firrelli jfirrelli@classicmodelcars.com 300, San Murphy, Edit | Update
Marketing
Francisco, President
CA, USA
Task 2
Extend the create and update employee scripts so that user can upload his/her profile picture
when creating or updating an employee. Any new record or any change in the record (update)
must be saved in the database.
https://www.w3schools.com/php/php_file_upload.asp
https://www.w3schools.com/php/php_file_create.asp
https://www.w3schools.com/php/php_file_open.asp
https://www.w3schools.com/php/php_file.asp
Task 3
Use super global arrays $_SESSION and $_COOKIE appropriately e.g. to store user’s information
in the session or to write cookies to store any important thing which can be useful in future.
Solution
Task 1:
<?php
$query = "select * from employees";
$data = mysqli_query($conn,$query);
$total = mysqli_num_rows($data);
if($total != 0)
{
while(($result = mysqli_fetch_assoc($data)))
{
if($result['picPath'] == NULL){
echo '<tr><td style="text-align:center; cursor: pointer;"><img src="employeePics/Default.png"
width="150px" height="auto" class="edit"/></td>';
}
else{
echo '<tr><td style="text-align:center; cursor: pointer;"><img src="'.$result['picPath'].'" width="150px"
height="auto" class="edit"/></td>';
}
echo "<td>".$result['employeeNumber']."</td>";
echo "<td>".$result['lastName']."</td>";
echo "<td>".$result['firstName']."</td>";
echo "<td>".$result['extension']."</td>";
echo "<td>".$result['email']."</td>";
echo "<td>".$result['officeCode']."</td>";
echo "<td>".$result['reportsTo']."</td>";
echo "<td>".$result['jobTitle']."</td>";
echo "<td><button class='edit btn btn-sm btn-primary'>Edit</button> <button class='delete btn btn-sm btn-
primary'>Delete</button></td></tr>";
}
}
else {
echo "No Records Found";
}
Task 2:
$employeeNumber = (int)$_POST["employeeNumber"];
$lastName = $_POST["lastName"];
$firstName = $_POST["firstName"];
$extension = $_POST["extension"];
$email = $_POST["email"];
$officeCode = (int)$_POST["officeCode"];
$reportsTo = (int)$_POST["reportsTo"];
$jobTitle = $_POST["jobTitle"];
// Image Upload Handling
if(isset($_FILES['pic'])){
// echo "<br><pre>";
// print_r($_FILES['pic']);
// echo "</pre><br>";
// Image Array
$img_name = $_FILES['pic']['name'];
$img_size = $_FILES['pic']['size'];
$tmp_name = $_FILES['pic']['tmp_name'];
$error = $_FILES['pic']['error'];
if(in_array($img_ex,$allowed_exs)){
$new_img_name = uniqid("EMP-",true).'.'.$img_ex; // To Avoid Duplication of name
$img_upload_path = 'employeePics/'. $new_img_name; // Path
move_uploaded_file($tmp_name,$img_upload_path); // Move form Temp folder to custom folder
}
else{
$pic_error = true;
//header("Location: /lab11/Task1 and Task2.php");
}
}
}
else{
$pic_error = true;
//header("Location: /lab11/Task1 and Task2");
}
}
// else{
// header("Location: /lab11/Task1 and Task2");
// }
if($pic_error == FALSE){
$sql = "INSERT INTO `employees` (`picPath`, `employeeNumber`, `lastName`, `firstName`, `extension`, `email`,
`officeCode`, `reportsTo`, `jobTitle`) VALUES ('$new_img_name', '$employeeNumber', '$lastName', '$firstName',
'$extension', '$email', '$officeCode', '$reportsTo', '$jobTitle')";
$result = mysqli_query($conn,$sql);
}
if($result){
//echo "The record has been successfully entered";
$insert = true;
Task 2 screenshot:
SESSION CODE
</html>
<body>
<?php
session_start();
if (isset ($_SESSION["username"]) && isset ($_SESSION["username"]))
{
$username=$_SESSION["username"];
$userID=$_SESSION["userid"];
}
else
{
$userID=339817;
$username="Hassan Haroon";
$_SESSION["username"]=$username;
$_SESSION["userid"]=$userID;
}
Cookies Code
<?php
$cookie_name = "Admin";
$cookie_value = "Hassan Haroon";
<?php
if (!isset ($_COOKIE [$cookie_name ]))
echo "Cookie name " . $cookie_name . " is not SET!";
else
{
echo "Cookie " . $cookie_name . " is SET! <br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Cookies Code 2
<html>
<body>
<?php
session_start();
if (isset ($_SESSION["username"]) && isset ($_SESSION["username"]))
{
$username = $_SESSION["username"];
</body>
</html>
Task 3 screenshot:
Cookies Set
Session Start
Session Continue