-
Notifications
You must be signed in to change notification settings - Fork 1
/
supplier-delete.php
70 lines (60 loc) · 2.1 KB
/
supplier-delete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
// Redirecting to registration page for GET requests
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
return header('Location: /index.php');
}
// Starting session
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// Preventing unauthorized access
if (!isset($_SESSION['user'])) {
$_SESSION['flash']['info'] = 'You are not logged in!';
return header('Location: /login.php');
} else {
/** @var Database */
$db = require_once './helpers/Database.php';
$conn = $db->getConnection();
$stmt = $conn->prepare('SELECT * FROM `users` WHERE `id` = :id');
$stmt->bindParam(':id', $_SESSION['user']);
$stmt->execute();
$user = $stmt->fetch();
if (empty($user)) {
$_SESSION['flash']['secondary'] = 'Please register your account again!';
return header('Location: /register.php');
}
if ($user->role !== 'admin') {
unset($_SESSION['user']);
$_SESSION['flash']['info'] = 'You are not authorized!';
return header('Location: /login.php');
}
}
// Validating ID
$supplierId = trim($_POST['id']);
if (empty($supplierId)) {
$_SESSION['flash']['danger'] = 'Supplier ID is empty!';
return header('Location: /index.php');
} else {
$stmt = $conn->prepare('SELECT `id` FROM `suppliers` WHERE `id` = :id LIMIT 1');
$stmt->bindParam(':id', $supplierId);
$stmt->execute();
if ($stmt->fetch() === false) {
$_SESSION['flash']['danger'] = 'Supplier not found!';
return header('Location: /index.php');
}
}
// Sanitizing data
unset($supplierId);
$id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
// Deleting current supplier
$stmt = $conn->prepare('DELETE FROM `suppliers` WHERE `id` = :id');
$stmt->bindParam(':id', $id);
try {
$stmt->execute();
} catch (Exception $e) {
$_SESSION['flash']['danger'] = 'Supplier cannot be deleted! One or more products are bound with it.';
return header('Location: /index.php');
}
// Redirecting to home page to display confirmation of supplier removal
$_SESSION['flash']['success'] = 'Supplier deleted successfully!';
return header('Location: /index.php');