Computer Networks and Technologies Final Job
Computer Networks and Technologies Final Job
Final Project
Computer Networks and Technologies
Donatas Sandonavicius
Lecturer
Kaunas, 2019
1. INTRODUCTION
The purpose of this project is to implement a system based on PHP and MySQL
databases.
The system is a electronic shop “ELECSHOP” with all features of a e-commerce
website and with a back-end admin and seller panel.
The website has a simple design using HTML and CSS.
2. How it works
2.1 Databases
We use SQL to store our databases. We create two databases one for the products of our
website and other for login.
2.2 Front-end
?>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/c4198f3bd1.js"
crossorigin="anonymous"></script>
</head>
<style>
llin{
color: green;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
}
</style>
<h1>
<a href="index.php">
ELECSHOP
</a>
</h1>
<ul>
<li><a href="products.php">Products</a></li>
<li><a href="login.php">Login</a></li>
</ul>
<span class="bag"><a href="cart.php"><i class="fas fa-shopping-
cart"></i></a><?=$bag?></span>
<body>
3
The home website index.php shows the menus of the website (“Products” and
“Login”) , a link to the shopping cart system and it lists the last two added products.
<?php
include("header.php");
?>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/c4198f3bd1.js"
crossorigin="anonymous"></script>
</head>
<body>
<?php
$pdo = pdo_connect_mysql();
$stmt = $pdo->prepare('SELECT * FROM products ORDER BY date_added DESC LIMIT
2');
$stmt->execute();
$recently_added_products = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="featured">
<h2>Last Added Products</h2>
<p>Discover our last products</p>
</div>
<div class="products">
<?php foreach ($recently_added_products as $product): ?>
<a href="product.php?page=product&id=<?=$product['id']?>"
class="product">
<img src="imgs/<?=$product['img']?>" width="200" height="200"
alt="<?=$product['name']?>">
<b> <?=$product['name']?>
€<?=$product['price']?>
</a>
<?php endforeach; ?>
</div>
</div>
</body>
Products shows us all the products in our website and we can filter them by name or
price range
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/c4198f3bd1.js"
crossorigin="anonymous"></script>
</head>
<body>
<?php
include("header.php");
?>
<?php
//error_reporting(0);
$pdo = pdo_connect_mysql();
}
?>
<div class="searchbox">
5
<label>Search name</label>
<input type="text" name="search" class="form-control" value="">
<label>Search price from</label>
<input type="text" name="prices" class="form-control" value="">
<label>to</label>
<input type="text" name="pricet" class="form-control" value="">
</div>
<span class="name"><?=$product['name']?></span>
<span class="price">
€<?=$product['price']?>
</span>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
<span class="name"><?=$product['name']?></span>
<span class="price">
€<?=$product['price']?>
</span>
</a>
<?php endforeach; ?>
<?php endif; ?>
<div class="buttons">
<?php if ($current_page > 1): ?>
<a href="product.php?page=products&p=<?=$current_page-1?>">Prev</a>
<?php endif; ?>
<?php if ($total_products > ($current_page *
$num_products_on_each_page) - $num_products_on_each_page + count($products)):
?>
<a href="product.php?page=products&p=<?=$current_page+1?>">Next</a>
<?php endif; ?>
</div>
</div>
</body>
The next system that we have created is the product page, where we can add our product
to the basket and see its characteristics
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/c4198f3bd1.js"
crossorigin="anonymous"></script>
</head>
<body>
<?php
include("header.php");
$pdo = pdo_connect_mysql();
// Check to make sure the id parameter is specified in the URL
if (isset($_GET['id'])) {
// Prepare statement and execute, prevents SQL injection
$stmt = $pdo->prepare('SELECT * FROM products WHERE id = ?');
$stmt->execute([$_GET['id']]);
// Fetch the product from the database and return the result as an Array
$product = $stmt->fetch(PDO::FETCH_ASSOC);
// Check if the product exists (array is not empty)
if (!$product) {
// Simple error to display if the id for the product doesn't exists
(array is empty)
die ('Product does not exist!');
}
} else {
// Simple error to display if the id wasn't specified
die ('Product does not exist error!');
}
?>
7
height="300" alt="<?=$product['name']?>">
<div>
</span><br><br>
<?php if ($product['rrp']==1): ?>
<span class="rrp">UPLOADED BY ADMIN</span>
<?php endif; ?>
<?php if ($product['rrp']==2): ?>
<span class="rrp">UPLOADED BY SELLER</span>
<?php endif; ?>
<div class="formulario">
<form action="cart.php" method="post">
<input type="number" name="quantity" value="1" min="1" max="<?
=$product['quantity']?>" placeholder="Quantity" required>
<input type="hidden" name="product_id" value="<?=$product['id']?
>">
<input type="submit" value="Add To Cart">
</form>
</div>
<div class="description">
<?=$product['desc']?>
</div>
</div>
</div>
</body>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/c4198f3bd1.js"
crossorigin="anonymous"></script>
</head>
<style>
table, th, td {
border: 1px solid black;
}
th {
background-color: #4CAF50;
color: white;
}
</style>
<body>
<?php
include("header.php");
// If the user clicked the add to cart button on the product page we can
check for the form data
if (isset($_POST['product_id'], $_POST['quantity']) &&
is_numeric($_POST['product_id']) && is_numeric($_POST['quantity'])) {
// Set the post variables so we easily identify them, also make sure they
are integer
$product_id = (int)$_POST['product_id'];
$quantity = (int)$_POST['quantity'];
// Prepare the SQL statement, we basically are checking if the product
exists in our databaser
$stmt = $pdo->prepare('SELECT * FROM products WHERE id = ?');
$stmt->execute([$_POST['product_id']]);
// Fetch the product from the database and return the result as an Array
$product = $stmt->fetch(PDO::FETCH_ASSOC);
//Save the products into a text file
// Remove product from cart, check for the URL param "remove", this is the
product id, make sure it's a number and check if it's in the cart
if (isset($_GET['remove']) && is_numeric($_GET['remove']) &&
isset($_SESSION['cart']) && isset($_SESSION['cart'][$_GET['remove']])) {
// Remove the product from the shopping cart
unset($_SESSION['cart'][$_GET['remove']]);
}
// Update product quantities in cart if the user clicks the "Update" button
on the shopping cart page
if (isset($_POST['update']) && isset($_SESSION['cart'])) {
// Loop through the post data so we can update the quantities for every
product in cart
foreach ($_POST as $k => $v) {
if (strpos($k, 'quantity') !== false && is_numeric($v)) {
$id = str_replace('quantity-', '', $k);
$quantity = (int)$v;
// Always do checks and validation
9
if (is_numeric($id) && isset($_SESSION['cart'][$id]) && $quantity
> 0) {
// Update new quantity
$_SESSION['cart'][$id] = $quantity;
}
}
}
}
// Send the user to the place order page if they click the Place Order
button, also the cart should not be empty
}
// Check the session variable for products in cart
$products_in_cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
$products = array();
$subtotal = 0.00;
// If there are products in cart
if ($products_in_cart) {
// There are products in the cart so we need to select those products
from the database
// Products in cart array to question mark string array, we need the SQL
statement to include IN (?,?,?,...etc)
$array_to_question_marks = implode(',', array_fill(0,
count($products_in_cart), '?'));
$stmt = $pdo->prepare('SELECT * FROM products WHERE id IN (' .
$array_to_question_marks . ')');
// We only need the array keys, not the values, the keys are the id's of
the products
$stmt->execute(array_keys($products_in_cart));
// Fetch the products from the database and return the result as an Array
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Calculate the subtotal
foreach ($products as $product) {
$subtotal += (float)$product['price'] * (int)
$products_in_cart[$product['id']];
}
}
?>
<div class="cart content-wrapper">
<h1>Shopping Cart</h1>
<form action="cart.php" method="post">
<table>
<thead>
<tr>
<td colspan="2">Product</td>
<td>Price</td>
<td>Quantity</td>
<td>Total</td>
</tr>
</thead>
<tbody>
<?php if (empty($products)): ?>
<tr>
<td colspan="5" style="text-align:center;">You have no
products added in your Shopping Cart</td>
</tr>
<?php else: ?>
<?php foreach ($products as $product): ?>
<tr>
<td class="img">
<a href="product.php?page=product&id=<?
=$product['id']?>">
<img src="imgs/<?=$product['img']?>" width="50"
height="50" alt="<?=$product['name']?>">
</a>
</td>
<td>
<a href="product.php?page=product&id=<?
=$product['id']?>"><?=$product['name']?></a>
<br>
<a href="cart.php?page=cart&remove=<?=$product['id']?
>" class="remove">Remove</a>
</td>
<td class="price">€<?=$product['price']?></td>
<td class="quantity">
<input type="number" name="quantity-<?
=$product['id']?>" value="<?=$products_in_cart[$product['id']]?>" min="1"
max="<?=$product['quantity']?>" placeholder="Quantity" required>
</td>
<td class="price">$<?=$product['price'] *
$products_in_cart[$product['id']]?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<div class="subtotal">
<span class="text">Subtotal</span>
<span class="price">$<?=$subtotal?></span>
</div>
<div class="buttons">
<input type="submit" value="Update" name="update">
11
<input type="submit" value="Place Order" name="placeorder" >
</div>
</form>
</div>
</body>
2.3 Back-end
When we press the login we have two kinds of users in the system “admin” and “seller”
, each of them have different privileges
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/c4198f3bd1.js"
crossorigin="anonymous"></script>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
body{ font: 14px sans-serif; }
.wrapper{ margin:10px auto;
display:block; width: 360px; padding: 20px; }
</style>
</head>
</html>
<?php
session_start();
// Include functions and connect to the database using PDO MySQL
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = 'root';
$DATABASE_NAME = 'elecshop';
// Check if the user is already logged in, if yes then redirect him to
welcome page
if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) {
header("location: index.php");
exit;
}
// Validate credentials
if(empty($username_err) && empty($password_err)){
// Prepare a select statement
$username=$_POST['username'];
$password=$_POST['password'];
session_start();
header("location: admin.php");
}
elseif(mysqli_num_rows($result)==1 && $usernamecheck!=1) {
session_start();
header("location: seller.php");
13
}
// Close statement
else{
echo "Incorrect password";
}
}
// Close connection
mysqli_close($conn);
}
?>
<html>
<body>
<div class="wrapper">
<h2>Login</h2>
<f>Please fill in your credentials to login.</f>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post">
<div class="form-group <?php echo (!empty($username_err)) ? 'has-
error' : ''; ?>">
<label>Username</label>
<input type="text" name="username" class="form-control" value="<?
php echo $username; ?>">
<span class="help-block"><?php echo $username_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($password_err)) ? 'has-
error' : ''; ?>">
<label>Password</label>
<input type="password" name="password" class="form-control"
value="<?php echo $password; ?>" >
<span class="help-block"><?php echo $password_err; ?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Login">
</div>
</form>
</div>
</body>
</html>
<?php
// Initialize the session
session_start();
The admin and seller panel show what do we have in our database products. In case of
admin, he can see products added by him and the sellers. On the other hand, sellers can
only see the products they have added.
admin.php
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/c4198f3bd1.js"
crossorigin="anonymous"></script>
</head>
<style>
a{
background-color: #f44336;
color: white;
padding: 14px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
}
table, th, td {
border: 1px solid black;
}
th {
background-color: #4CAF50;
color: white;
}
</style>
<body>
<?php
session_start();
// Include functions and connect to the database using PDO MySQL
include 'functions.php';
$pdo = pdo_connect_mysql();
15
?>
<table style="width:100%">
<tr>
<th>Product</th>
<th>Image</th>
<th>Quantity</th>
<th>Price</th>
<th>1=Admin/2=Seller</th>
</tr>
<?php foreach ($products as $product): ?>
<tr>
<td> <?=$product['name']?> </td>
<td> <img class="products" src="imgs/<?=$product['img']?>"
width="200" height="200" alt="<?=$product['name']?>"></td>
<td> <?=$product['quantity']?>
</td>
<td> €<?=$product['price']?>
</td>
<td> <?=$product['rrp']?>
</td>
</tr>
</table>
</form>
<a href="add.php">Add Product</a>
<a href="update.php">Update Product</a>
<a href="remove.php">Remove Product</a>
<a href="index.php"><i class="fas fa-home"></i></a>
</body>
seller.php
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/c4198f3bd1.js"
crossorigin="anonymous"></script>
</head>
<style>
a{
background-color: #f44336;
color: white;
padding: 14px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
}
table, th, td {
border: 1px solid black;
}
th {
background-color: #4CAF50;
color: white;
}
</style>
<body>
<?php
session_start();
// Include functions and connect to the database using PDO MySQL
include 'functions.php';
$pdo= pdo_connect_mysql();
?>
<table style="width:100%">
<tr>
<th>Product</th>
<th>Image</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<?php foreach ($products as $product): ?>
<tr>
<td> <?=$product['name']?> </td>
<td> <img class="products" src="imgs/<?
17
=$product['img']?>" width="200" height="200" alt="<?=$product['name']?
>"></td>
<td> <?=$product['quantity']?>
</td>
<td> €<?=$product['price']?>
</td>
</tr>
</table>
</form>
<a href="add.php">Add Product</a>
<a href="remove.php">Remove Product</a>
</body>
In the admin panel we have three functions: add product, delete product and update
product.
add.php
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/c4198f3bd1.js"
crossorigin="anonymous"></script>
</head>
<body>
<?php
//IMAGE UPLOAD
if(isset($_POST['submit'])) {
$currentDir = getcwd();
$uploadDirectory = "/imgs/";
$fileName = $_FILES['myfile']['name'];
$fileSize = $_FILES['myfile']['size'];
$fileTmpName = $_FILES['myfile']['tmp_name'];
$fileType = $_FILES['myfile']['type'];
$fileExtension = strtolower(end(explode('.', $fileName)));
if (isset($_POST['submit'])) {
if (!in_array($fileExtension, $fileExtensions)) {
$errors[] = "This file extension is not allowed. Please upload a
JPEG or PNG file";
}
if (empty($errors)) {
$didUpload = move_uploaded_file($fileTmpName, $uploadPath);
if ($didUpload) {
echo "The file " . basename($fileName) . " has been
uploaded";
} else {
echo "An error occurred somewhere. Try again or contact the
admin";
}
} else {
foreach ($errors as $error) {
echo $error . "These are the errors" . "\n";
}
}
}
}
session_start();
// Include functions and connect to the database using PDO MySQL
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = 'root';
$DATABASE_NAME = 'elecshop';
19
}elseif(isset($_POST['submit'])&& $_SESSION["id"] !=1){
}
mysqli_close($conn);
$name= "";
$price= "";
$description= "";
$quantity="";
?>
<div class="formulario">
Upload a File:
<input type="file" name="myfile" id="fileToUpload">
<label>Send</label>
</div>
<?php
if (isset($_POST['submit']))
{
header('Location:add.php');
}
?>
<llin><a href="admin.php"><?php if (isset($_SESSION["loggedin"]) &&
$_SESSION["loggedin"] === true && $_SESSION["id"] ==1){echo "ADMIN PANEL"; }?
></a></llin>
<llin><a href="seller.php"><?php if (isset($_SESSION["loggedin"]) &&
$_SESSION["loggedin"] === true && $_SESSION["id"] ==2){echo "SELLER PANEL";
}?></a></llin>
</body>
remove.php
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/c4198f3bd1.js"
crossorigin="anonymous"></script>
</head>
<body>
<?php
session_start();
// Include functions and connect to the database using PDO MySQL
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = 'root';
$DATABASE_NAME = 'elecshop';
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['submit'])&& $_SESSION["id"] ==1) {
$name=$_POST['name'];
$sql = "DELETE FROM products WHERE name='$name'";
if (mysqli_query($conn, $sql)) {
echo "Deleted succesfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}elseif (isset($_POST['submit'])&& $_SESSION["id"] !=1) {
$name = $_POST['name'];
$sql = "DELETE FROM products WHERE name='$name' AND rrp='2'";
if (mysqli_query($conn, $sql)) {
echo "Deleted succesfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
?>
<div class="formulario">
21
<form action="" method="post">
<h2>Introduce the name of the product you want to remove</h2>
<label>Name</label>
<input type="text" name="name" class="form-control" value=""><br>
<label>Send</label>
</form>
</div>
<llin><a href="admin.php"><?php if (isset($_SESSION["loggedin"]) &&
$_SESSION["loggedin"] === true && $_SESSION["id"] ==1){echo "ADMIN PANEL"; }?
></a></llin>
<llin><a href="seller.php"><?php if (isset($_SESSION["loggedin"]) &&
$_SESSION["loggedin"] === true && $_SESSION["id"] ==2){echo "SELLER PANEL";
}?></a></llin>
</body>
update.php
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/c4198f3bd1.js"
crossorigin="anonymous"></script>
</head>
<body>
<?php
error_reporting(0);
session_start();
// Include functions and connect to the database using PDO MySQL
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = 'root';
$DATABASE_NAME = 'elecshop';
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$selection = "SELECT * FROM products WHERE name='$name'";
$result = mysqli_query($conn, $selection);
$product = mysqli_fetch_array($result, MYSQLI_ASSOC);
}
if (isset($_POST['submit2'])) {
$id=$_POST['id'];
$sql = "UPDATE `products`
SET
`name`='$_POST[name2]',`desc`='$_POST[description]',`price`='$_POST[price]',`
quantity`='$_POST[quantity]' WHERE `id`='$id'";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
?>
<div class="formulario">
<label>Send</label>
23
<input name="submit2" type="submit" class="btn btn-primary"
value="submit">
<?php
function pdo_connect_mysql() {
// Update the details below with your MySQL details
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = 'root';
$DATABASE_NAME = 'elecshop';
try {
return new PDO('mysql:host=' . $DATABASE_HOST . ';dbname=' .
$DATABASE_NAME . ';charset=utf8', $DATABASE_USER, $DATABASE_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
// If there is an error with the connection, stop the script and
display the error.
}
}
function bag_function(){
$num_items_in_cart = isset($_SESSION['cart']) ? count($_SESSION['cart']) : 0;
return $num_items_in_cart;
}
?>