-
Notifications
You must be signed in to change notification settings - Fork 1
/
supplier-store.php
72 lines (60 loc) · 2.06 KB
/
supplier-store.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
71
72
<?php
// Redirecting to registration page for GET requests
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
return header('Location: /supplier-add.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 name
if (empty(trim($_POST['name']))) {
$_SESSION['error']['name'] = 'Name cannot be empty!';
}
// Validating address
if (empty(trim($_POST['address']))) {
$_SESSION['error']['address'] = 'Address cannot be empty!';
}
// Redirecting to supplier add page to display errors
if (isset($_SESSION['error'])) {
$_SESSION['input'] = [
'name' => $_POST['name'],
'address' => $_POST['address'],
];
return header('Location: /supplier-add.php');
}
// Sanitizing data
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$address = filter_input(INPUT_POST, 'address', FILTER_SANITIZE_STRING);
// Inserting new supplier into database
$stmt = $conn->prepare(
'INSERT INTO `suppliers` (`name`, `address`) VALUES (:name, :address)'
);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':address', $address);
$stmt->execute();
// Redirecting to home page to display confirmation of supplier addition
$_SESSION['flash']['success'] = 'Supplier added successfully!';
return header('Location: /index.php');