Web Engineering Lab 13
Web Engineering Lab 13
Lab Task
Notes:
Task 1:
config.php:
<?php
$dbhost = 'localhost';
$dbusername="root";
$dbpassword="";
$dbname="classicmodels";
$mysqli = mysqli_connect($dbhost,$dbusername,$dbpassword,$dbname);
?>
<?php
include("./config/config.php");
$employees = mysqli_query($mysqli,"SELECT * FROM employees LEFT JOIN offices ON
employees.officeCode = offices.officeCode ORDER BY employeeNumber;");
?>
<html>
<head>
<title>LAB11-HOMEPAGE</title>
<style>
th,td{
padding: 10px;
}
</style>
</head>
<body>
<a href="add.html">ADD NEW EMPLOYEE</a><br>
<h1>Employee Table</h1>
<table>
<tr bgcolor='#CCCCCC'>
<th>Name</th>
<th>Email</th>
<th>Job Title</th>
<th>Emp Office Address</th>
<th>Reports To</th>
<th>Update</th>
</tr>
<?php
while($emp = mysqli_fetch_array($employees)) {
echo "<tr>";
echo "<td>".$emp['firstName']." ".$emp['lastName']."</td>";
echo "<td>".$emp['email']."</td>";
echo "<td>".$emp['jobTitle']."</td>";
echo "<td>".$emp['addressLine1']."<br>".
$emp['addressLine2']."<br>".$emp['city'].",".$emp['state'].",".
$emp['country']."</td>";
$id = $emp['reportsTo'];
$repto_= mysqli_query($mysqli,"SELECT * FROM employees WHERE emplo
yeeNumber=$id;");
}else{
while($repto = mysqli_fetch_array($repto_)){
echo "<td>".$repto['firstName']." ".
$repto['lastName'].",".$repto['jobTitle']."</td>";
}
}
echo "<td><a href=\"edit.php?id=$emp[employeeNumber]\">Edit</a> |
<a href=\"delete.php?id=$emp[employeeNumber]\" onClick=\"return confirm('Are yo
u sure you want to delete?')\">Delete</a></td>";
}
$mysqli->close();
?>
</table>
</body>
</html>
<html>
<head>
<title>Add New Employee</title>
</head>
<body>
<a href="index.php">Home</a>
<br /><br />
<form action="add.php" method="post" name="form1">
<table width="25%" border="0">
<tr>
<td>Employee Number</td>
<td><input type="number" name="employeeNumber" /></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="firstName" /></td>
add.php:
<html>
<head>
<title>Add Data</title>
</head>
<body>
<?php
if(isset($_POST['Submit'])) {
$employeeNumber = mysqli_escape_string($mysqli,$_POST['employeeNumber']);
$firstName = mysqli_real_escape_string($mysqli, $_POST['firstName']);
$lastName = mysqli_real_escape_string($mysqli, $_POST['lastName']);
$email = mysqli_real_escape_string($mysqli, $_POST['email']);
$jobTitle = mysqli_real_escape_string($mysqli, $_POST['jobTitle']);
$officeCode = mysqli_real_escape_string($mysqli,$_POST['officeCode']);
$reportsTo = mysqli_real_escape_string($mysqli,$_POST['reportsTo']);
// checking empty fields
if(empty($firstName) || empty($lastName) || empty($email) || empty($jobTit
le) || empty($officeCode) || empty($reportsTo)) {
echo "<font color='red'>Please fill all the fields!!!</font><br/>";
//link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
// if all the fields are filled (not empty)
//insert data to database
$result = mysqli_query($mysqli, "INSERT INTO employees(employeeNumber,f
irstName,lastName,email,jobTitle,officeCode,reportsTo,extension) VALUES($employ
eeNumber,'$firstName','$lastName','$email','$jobTitle',$officeCode,
$reportsTo,'x5800');");
//display success message
echo "<font color='green'>Data added successfully.";
echo "<br/><a href='index.php'>View Result</a>";
}
$mysqli->close();
}
?>
</body>
</html>
<?php
//including the database connection file
include(".\config\config.php");
//getting id of the data from url
$id = $_GET['id'];
//deleting the row from table
$result = mysqli_query($mysqli, "DELETE FROM employees WHERE employeeNumber=$id
");
$mysqli->close();
//redirecting to the display page (index.php in our case)
header("Location:index.php");
?>
<?php
// including the database connection file
include_once(".\config\config.php");
if(isset($_POST['update'])){
$employeeNumber = mysqli_escape_string($mysqli,$_POST['employeeNumber']);
$firstName = mysqli_real_escape_string($mysqli, $_POST['firstName']);
$lastName = mysqli_real_escape_string($mysqli, $_POST['lastName']);
//updating the table
$result = mysqli_query($mysqli, "UPDATE employees SET firstName='$firs
tName',lastName='$lastName',jobTitle='$jobTitle',email='$email',officeCode=$off
iceCode,reportsTo=$reportsTo WHERE employeeNumber=$employeeNumber");
$mysqli->close();
//redirectig to the display page. In our case, it is index.php
header("Location: index.php");
}
}
?>
<?php
//getting id from url
$id = $_GET['id'];
//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM employees WHERE employeeNumber=$
id");
while($res = mysqli_fetch_array($result)){
$employeeNumber = $res['employeeNumber'];
$firstName = $res['firstName'];
$lastName = $res['lastName'];
$email = $res['email'];
$jobTitle = $res['jobTitle'];
$officeCode =$res['officeCode'];
$reportsTo =$res['reportsTo'];
}
<html>
<head>
<title>Edit Data</title>
</head>
<body>
<a href="index.php">Home</a>
<br/><br/>
<form name="form1" method="post" action="edit.php">
<table border="0">
<tr>
<td>Employee Number</td>
<td><input type="number" name="employeeNumber" value="<?php echo $
employeeNumber;?>"/></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="firstName" value="<?php echo $firstNam
e;?>"/></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lastName" value="<?php echo $lastName;?
>"/></td>
</tr>
<tr>
<td>Job Title</td>
<td><input type="text" name="jobTitle" value="<?php echo $jobTitle;?
>"/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="email" name="email" value="<?php echo $email;?
>"/></td>
</tr>
<tr>
<td>Office Code</td>