Commit a3e27d7c authored by Sasho Gjorgiev's avatar Sasho Gjorgiev
Browse files

challenge finished

No related merge requests found
Showing with 628 additions and 0 deletions
+628 -0
<?php
namespace Database;
use PDO;
class Connection
{
const HOST = 'localhost';
const DBNAME = 'ch_17';
const USERNAME = 'root';
const PASSWORD = '';
protected $connection;
public function __construct()
{
$this->connection = new PDO('mysql:host=' . self::HOST . ';dbname=' . self::DBNAME, self::USERNAME,self::PASSWORD);
}
public function getConnection()
{
return $this->connection;
}
public function destroy()
{
$this->connection = null;
}
}
\ No newline at end of file
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
require_once(__DIR__ . '/Database/Connection.php');
$connectionObj = new \Database\Connection();
$connection = $connectionObj->getConnection();
$modelOptions = [];
$modelQuery = $connection->query("SELECT * FROM vehicle_models");
if ($modelQuery) {
$modelOptions = $modelQuery->fetchAll(PDO::FETCH_ASSOC);
}
$fuelOptions = [];
$fuelQuery = $connection->query("SELECT * FROM fuel_types");
if ($fuelQuery) {
$fuelOptions = $fuelQuery->fetchAll(PDO::FETCH_ASSOC);
}
$vehicleTypeOptions = ['sedan', 'coupe', 'hatchback', 'suv', 'minivan'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
<meta charset="utf-8" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!-- Latest compiled and minified Bootstrap 4.6 CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<!-- CSS script -->
<link rel="stylesheet" href="style.css">
<!-- Latest Font-Awesome CDN -->
<script src="https://kit.fontawesome.com/3257d9ad29.js" crossorigin="anonymous"></script>
</head>
<body>
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand">Vehicle Registration</a>
<form class="form-inline" method="post" action="logout.php">
<button class="btn btn-outline-primary my-2 my-sm-0" type="submit">Logout</button>
</form>
</nav>
<div class="container">
<h1 class="text-center">Vehicle Registration Form</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" class="two-column-form">
<div class="row">
<div class="col-6">
<div class="form-group">
<label for="vehicle_model">Vehicle Model:</label> <br>
<select name="vehicle_model_id" id="vehicle_model" class="form-control">
<?php foreach ($modelOptions as $model) : ?>
<option value="<?= $model['id'] ?>"><?= $model['name'] ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="vehicle_chassis_number">Vehicle Chassis Number:</label>
<input type="text" name="vehicle_chassis_number" id="vehicle_chassis_number" class="form-control">
</div>
<div class="form-group">
<label for="registration_number">Registration Number:</label>
<input type="text" name="registration_number" id="registration_number" class="form-control">
</div>
<div class="form-group">
<label for="registration_to">Registration To:</label>
<input type="date" name="registration_to" id="registration_to" class="form-control">
</div>
</div>
<div class="col-6">
<div class="form-group">
<label for="vehicle_type">Vehicle Type:</label>
<select name="vehicle_type" id="vehicle_type" class="form-control">
<?php foreach ($vehicleTypeOptions as $type) : ?>
<option value="<?= $type ?>"><?= $type ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="vehicle_production_year">Vehicle Production Year:</label>
<input type="date" name="vehicle_production_year" id="vehicle_production_year" class="form-control">
</div>
<div class="form-group">
<label for="fuel_type">Fuel Type:</label>
<select name="fuel_type_id" id="fuel_type_id" class="form-control">
<?php foreach ($fuelOptions as $fuel) : ?>
<option value="<?= $fuel['id'] ?>"><?= $fuel['name'] ?></option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary px-5">Submit</button>
</div>
</div>
</form>
</div>
<?php
$vehicle_type = isset($_POST['vehicle_type']) ? $_POST['vehicle_type'] : '';
$vehicle_chassis_number = isset($_POST['vehicle_chassis_number']) ? $_POST['vehicle_chassis_number'] : '';
$vehicle_production_year = isset($_POST['vehicle_production_year']) ? $_POST['vehicle_production_year'] : '';
$registration_number = isset($_POST['registration_number']) ? $_POST['registration_number'] : '';
$registration_to = isset($_POST['registration_to']) ? $_POST['registration_to'] : '';
$vehicle_model_id = isset($_POST['vehicle_model_id']) ? $_POST['vehicle_model_id'] : '';
$fuel_type_id = isset($_POST['fuel_type_id']) ? $_POST['fuel_type_id'] : '';
$query = $connection->prepare("SELECT COUNT(*) FROM vehicle_registration WHERE vehicle_chassis_number = ?");
$query->execute([$vehicle_chassis_number]);
$count = $query->fetchColumn();
if ($count > 0) {
echo "<h5 style='color: red;'>Chassis Number already exists in the database.</h5>";
} else {
$vehicle_type = isset($_POST['vehicle_type']) ? $_POST['vehicle_type'] : '';
$vehicle_chassis_number = isset($_POST['vehicle_chassis_number']) ? $_POST['vehicle_chassis_number'] : '';
$vehicle_production_year = isset($_POST['vehicle_production_year']) ? $_POST['vehicle_production_year'] : '';
$registration_number = isset($_POST['registration_number']) ? $_POST['registration_number'] : '';
$registration_to = isset($_POST['registration_to']) ? $_POST['registration_to'] : '';
try {
$insertQuery = $connection->prepare("INSERT INTO vehicle_registration (vehicle_type, vehicle_chassis_number, vehicle_production_year, registration_number, registration_to, vehicle_model_id, fuel_type_id) VALUES (?, ?, ?, ?, ?, ?, ?)");
$insertQuery->bindParam(1, $vehicle_type);
$insertQuery->bindParam(2, $vehicle_chassis_number);
$insertQuery->bindParam(3, $vehicle_production_year);
$insertQuery->bindParam(4, $registration_number);
$insertQuery->bindParam(5, $registration_to);
$insertQuery->bindParam(6, $vehicle_model_id);
$insertQuery->bindParam(7, $fuel_type_id);
$insertQuery->execute();
echo "<h5 style='color: green;'>Registration successful!</h5>";
} catch (PDOException $e) {
die('Database error: ' . $e->getMessage());
}
}
?>
<form class="form-inline" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input class="form-control ml-auto mr-2" type="text" name="search_query" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0 mr-2" type="submit">Search</button>
</form>
<?php
if (isset($_POST['search'])) {
$searchQuery = $_POST['search_query'];
$search = "SELECT
vr.*,
vm.name AS vehicle_models_name,
vt.name AS fuel_types_name
FROM
vehicle_registration vr
LEFT JOIN
vehicle_models vm ON vr.vehicle_model_id = vm.id
LEFT JOIN
fuel_types vt ON vr.fuel_type_id = vt.id
WHERE
vr.vehicle_model_id LIKE :search_query
OR vr.registration_number LIKE :search_query
OR vr.vehicle_chassis_number LIKE :search_query";
$stmt = $connection->prepare($search);
$stmt->bindValue(':search_query', '%' . $searchQuery . '%', PDO::PARAM_STR);
$stmt->execute();
$licensedVehicles = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
<?php
$query = "SELECT
vr.*,
vm.name AS vehicle_models_name,
vt.name AS fuel_types_name
FROM
vehicle_registration vr
LEFT JOIN
vehicle_models vm ON vr.vehicle_model_id = vm.id
LEFT JOIN
fuel_types vt ON vr.fuel_type_id = vt.id";
try {
$stmt = $connection->query($query);
$licensedVehicles = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die('Database error: ' . $e->getMessage());
}
?>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">vehicle_model</th>
<th scope="col">vehicle_type</th>
<th scope="col">vehicle_chassis_number</th>
<th scope="col">vehicle_production_year</th>
<th scope="col">registration_number</th>
<th scope="col">fuel_type</th>
<th scope="col">registration_to</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($licensedVehicles as $vehicle) : ?>
<?php
$currentDate = new DateTime();
$registrationToDate = new DateTime($vehicle['registration_to']);
$diff = $currentDate->diff($registrationToDate);
$rowClass = '';
if ($diff->days < 30) {
$rowClass = 'table-warning';
} elseif ($currentDate > $registrationToDate) {
$rowClass = 'table-danger';
}
?>
<tr class="<?= $rowClass ?>">
<td><?= $vehicle['id'] ?></td>
<td><?= $vehicle['vehicle_models_name'] ?></td>
<td><?= $vehicle['vehicle_type'] ?></td>
<td><?= $vehicle['vehicle_chassis_number'] ?></td>
<td><?= $vehicle['vehicle_production_year'] ?></td>
<td><?= $vehicle['registration_number'] ?></td>
<td><?= $vehicle['fuel_types_name'] ?></td>
<td><?= $vehicle['registration_to'] ?></td>
<form action="edit-user.php" method="POST">
<td>
<input type="hidden" name="id" value="<?= $vehicle['id'] ?>">
<button type="submit" class="btn btn-warning">Edit</button>
</td>
</form>
<form action="delete-user.php" method="POST">
<td> <input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?= $vehicle['id'] ?>">
<button type="submit" class="btn btn-danger">Delete</button>
</td>
</form>
<td>
<?php
if ($rowClass === 'table-warning' || $rowClass === 'table-danger') {
echo '<a href="extend.php?id=' . $vehicle['id'] . '" class="btn btn-success btn-sm">Extend</a>';
}
?>
</td>
</td>
<?php endforeach; ?>
</tbody>
</table>
<!-- jQuery library -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="ha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<!-- Latest Compiled Bootstrap 4.6 JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.min.js" integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous"></script>
</body>
</html>
\ No newline at end of file
<?php
require_once(__DIR__ . '/Database/Connection.php');
use Database\Connection as Connection;
$connectionObj = new Connection;
$connection = $connectionObj->getConnection();
if (isset($_POST['id'])) {
$statement = $connection->prepare('DELETE FROM vehicle_registration where id=:id');
$statement->bindParam(":id", $_POST['id']);
$statement->execute();
header('Location: dashboard.php');
} else {
header('Location: dashboard.php?Something%20Went%20wrong');
}
<?php
require_once(__DIR__ . '/Database/Connection.php');
use Database\Connection as Connection;
$connectionObj = new Connection;
$connection = $connectionObj->getConnection();
if (isset($_POST['id'])) {
$id = $_POST['id'];
$checkQuery = $connection->prepare('SELECT COUNT(*) FROM vehicle_registration WHERE id = ?');
$checkQuery->execute([$id]);
$count = $checkQuery->fetchColumn();
if ($count > 0) {
$vehicle_type = $_POST['vehicle_type'] ? $_POST['vehicle_type'] : '';
$vehicle_chassis_number = $_POST['vehicle_chassis_number'] ? $_POST['vehicle_chassis_number'] : '';
$vehicle_production_year = $_POST['vehicle_production_year'] ? $_POST['vehicle_production_year'] : '';
$registration_number = $_POST['registration_number'] ? $_POST['registration_number'] : '';
$registration_to = $_POST['registration_to'] ? $_POST['registration_to'] : '';
$statement = $connection->prepare('UPDATE vehicle_registration SET
vehicle_type = :vehicle_type,
vehicle_chassis_number = :vehicle_chassis_number,
vehicle_production_year = :vehicle_production_year,
registration_number = :registration_number,
registration_to = :registration_to
WHERE id = :id');
$statement->bindParam(":id", $id);
$statement->bindParam(":vehicle_type", $vehicle_type);
$statement->bindParam(":vehicle_chassis_number", $vehicle_chassis_number);
$statement->bindParam(":vehicle_production_year", $vehicle_production_year);
$statement->bindParam(":registration_number", $registration_number);
$statement->bindParam(":registration_to", $registration_to);
$statement->execute();
header('Location: dashboard.php');
} else {
header('Location: dashboard.php?error=Invalid%20ID');
}
} else {
header('Location: dashboard.php?Something%20Went%20wrong');
}
extend.php 0 → 100644
<?php
require_once(__DIR__ . '/Database/Connection.php');
use Database\Connection as Connection;
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['id'])) {
$connectionObj = new Connection;
$connection = $connectionObj->getConnection();
$id = $_GET['id'];
$checkQuery = $connection->prepare('SELECT * FROM vehicle_registration WHERE registration_to = ?');
$checkQuery->execute([$id]);
$vehicle = $checkQuery->fetch(PDO::FETCH_ASSOC);
if ($vehicle) {
echo '<form action="update-registration.php" method="POST">';
echo '<input type="hidden" name="id" value="' . $id . '">';
echo '<label for="registration_to">New Registration To Date:</label>';
echo '<input type="date" name="registration_to" id="registration_to" value="' . $vehicle['registration_to'] . '">';
echo '<button type="submit">Extend Registration</button>';
echo '</form>';
} else {
echo 'Record not found.';
}
} else {
echo 'Invalid request or missing ID.';
}
index.php 0 → 100644
<!DOCTYPE html>
<html>
<head>
<title>Vehicles</title>
<meta charset="utf-8" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!-- Latest compiled and minified Bootstrap 4.6 CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<!-- CSS script -->
<link rel="stylesheet" href="style.css">
<!-- Latest Font-Awesome CDN -->
<script src="https://kit.fontawesome.com/3257d9ad29.js" crossorigin="anonymous"></script>
</head>
<body>
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand">Vehicle Registration</a>
<form class="form-inline" method="post" action="users.php">
<button class="btn btn-outline-primary my-2 my-sm-0" type="submit">Login</button>
</form>
</nav>
<div class="container">
<div class="jumbotron text-center">
<h1 class="display-4">Vehicle Registration</h1>
<p class="lead"> Enter your registration number to check its validity</p>
<form action="page2.php" method="POST">
<input type="text" name="search" id="search" placeholder="Registration number" class=" px-5"> <br>
<button class="btn btn-primary submit mt-3">Search</button>
</form>
</div>
</div>
<!-- jQuery library -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="ha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<!-- Latest Compiled Bootstrap 4.6 JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.min.js" integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous"></script>
</body>
</html>
\ No newline at end of file
login.php 0 → 100644
<?php
session_start();
require_once(__DIR__ . '/Database/Connection.php');
$connectionObj = new \Database\Connection();
$connection = $connectionObj->getConnection();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $connection->prepare("SELECT id, username, password FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && $user['password']) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header('Location: dashboard.php');
exit();
} else {
echo "Invalid username or password.";
}
}
?>
<?php
session_start();
session_unset();
session_destroy();
header('Location: users.php');
exit();
?>
page2.php 0 → 100644
<?php
use Database\Connection as Connection;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
require_once(__DIR__ . '/Database/Connection.php');
$connectionObj = new Connection();
$connection = $connectionObj->getConnection();
$vehicle_type = isset($_POST['vehicle_type']) ? $_POST['vehicle_type'] : '';
$vehicle_chassis_number = isset($_POST['vehicle_chassis_number']) ? $_POST['vehicle_chassis_number'] : '';
$vehicle_production_year = isset($_POST['vehicle_production_year']) ? $_POST['vehicle_production_year'] : '';
$registration_number = isset($_POST['registration_number']) ? $_POST['registration_number'] : '';
$vehicle_model_id = isset($_POST['vehicle_model_id']) ? $_POST['vehicle_model_id'] : '';
$fuel_type_id = isset($_POST['fuel_type_id']) ? $_POST['fuel_type_id'] : '';
$registration_to = isset($_POST['registration_to']) ? $_POST['registration_to'] : '';
$license_plate = $_POST['search'];
$query = $connection->prepare("SELECT * FROM vehicle_registration WHERE registration_number = ?");
$query->execute([$license_plate]);
if ($query->rowCount() > 0) {
echo "<h3>Vehicle Information</h3>";
echo "<table class='table table-bordered'>
<thead>
<tr>
<th>Vehicle Model</th>
<th>Vehicle Type</th>
<th>Chassis Number</th>
<th>Production Year</th>
<th>Registration Number</th>
<th>Fuel Type</th>
<th>Registration To</th>
</tr>
</thead>
<tbody>";
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>
<td>{$row['vehicle_model_id']}</td>
<td>{$row['vehicle_type']}</td>
<td>{$row['vehicle_chassis_number']}</td>
<td>{$row['vehicle_production_year']}</td>
<td>{$row['registration_number']}</td>
<td>{$row['fuel_type_id']}</td>
<td>{$row['registration_to']}</td>
</tr>";
}
echo "</tbody></table>";
} else {
echo "<p style='color: red;'>No such record found.</p>";
}
} catch (PDOException $e) {
die('Database error: ' . $e->getMessage());
}
}
?>
\ No newline at end of file
<?php
require_once(__DIR__ . '/Database/Connection.php');
use Database\Connection as Connection;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'], $_POST['registration_to'])) {
$connectionObj = new Connection;
$connection = $connectionObj->getConnection();
$id = $_POST['id'];
$newRegistrationTo = $_POST['registration_to'];
$updateQuery = $connection->prepare('UPDATE vehicle_registration SET registration_to = ? WHERE id = ?');
if ($updateQuery->execute([$newRegistrationTo, $id])) {
header('Location: dashboard.php');
} else {
echo 'Error updating the registration date.';
}
} else {
echo 'Invalid request or missing data.';
}
?>
users.php 0 → 100644
<?php
?>
<!DOCTYPE html>
<html>
<head>
<title >Users</title>
<meta charset="utf-8" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!-- Latest compiled and minified Bootstrap 4.6 CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<!-- CSS script -->
<link rel="stylesheet" href="style.css">
<!-- Latest Font-Awesome CDN -->
<script src="https://kit.fontawesome.com/3257d9ad29.js" crossorigin="anonymous"></script>
</head>
<body>
<form action="login.php" method="post" class="mt-3">
<label for="username">Username
</label>
<input type="text" name="username" id="username"> <br>
<label for="password">Password</label>
<input type="password" name="password" id="password"> <br>
<button class=" submit btn btn-primary">Send</button>
</form>
<!-- jQuery library -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="ha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<!-- Latest Compiled Bootstrap 4.6 JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.min.js" integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous"></script>
</body>
</html>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment