-
Notifications
You must be signed in to change notification settings - Fork 1
/
branchDelete.php
73 lines (61 loc) · 2.25 KB
/
branchDelete.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
73
<?php
// Start the session
session_start();
// Check if the user is logged in
if (!isset($_SESSION['email'])) {
// If not logged in, redirect to login page
header("Location: pages-login.php");
exit();
}
// Include the database connection
include 'config/db.php';
// Get session email
$email = $_SESSION['email'];
// Get user name, email, and role from the register table
$getAdminData = "SELECT * FROM register WHERE email = '$email'";
$resultData = mysqli_query($conn, $getAdminData);
if ($resultData->num_rows > 0) {
$row2 = $resultData->fetch_assoc();
$adminName = $row2['name'];
$adminEmail = $row2['email'];
$userRole = $row2['role']; // Assuming you have a 'role' column in the 'register' table
}
// Check if the user is an admin, otherwise redirect
if (isset($_SESSION['role']) && $_SESSION['role'] != 'admin') {
// If the user is not an Admin, redirect to index page
header("Location: index.php");
exit();
}
if (isset($_GET['id'])) {
$branch_id = intval($_GET['id']); // Ensure branch ID is an integer
// Get the number of branches for the specific box
$queryCount = "SELECT COUNT(*) AS Acc3_count FROM departments WHERE branch_id_fk = '$branch_id'";
$resultCount = mysqli_query($conn, $queryCount);
$rowCount = mysqli_fetch_assoc($resultCount);
$Count = $rowCount['Acc3_count'];
if ($Count > 0) {
echo 'Error: Cannot delete a branch having departments. Please delete the departments first.';
}
// Fetch the company ID associated with the branch from the compID_FK column
$sql = "SELECT `comp_id_fk` FROM `branches` WHERE `branch_id` = $branch_id";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
$row = $result->fetch_assoc();
$company_id = $row['comp_id_fk'];
// Now, perform the delete operation
$delete_sql = "DELETE FROM `branches` WHERE `branch_id` = $branch_id";
if ($conn->query($delete_sql) === TRUE) {
// Redirect to the company's branches page after successful deletion
header("Location: Branches.php?id=" . $company_id);
exit;
} else {
echo "Error deleting record: " . $conn->error;
}
} else {
echo "Error: No company found for this branch.";
}
// Close the database connection
$conn->close();
} else {
echo "No branch ID provided.";
}