-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_category.php
163 lines (113 loc) · 5.04 KB
/
admin_category.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
$title = 'Add Book Category';
include './php/config.php';
session_start();
$admin_id = $_SESSION['admin_id'];
if(!isset($admin_id)){
header('location:login.php');
}
if(isset($_GET['delete'])){
$id = $_GET['delete'];
$sql = "DELETE FROM categories WHERE id = '$id'";
$result = mysqli_query($con, $sql);
if($result){
echo '<script>alert("Book Category Deleted Successfully!");
window.location.href="admin_category.php";</script>';
} else {
echo '<script>alert("Book Category not deleted!")</script>';
}
}
if(isset($_POST['update'])){
$update_id = $_POST['update_id'];
$update_cat_name = mysqli_real_escape_string($con, $_POST['update_name']);
$update_sql = mysqli_query($con, "UPDATE categories SET name = '$update_cat_name' WHERE id = '$update_id'") or die('query failed');
header('location:admin_category.php');
}
if(isset($_POST['addCategory'])){
$category = mysqli_real_escape_string($con, $_POST['category']);
$select_book_category = mysqli_query($con, "SELECT * FROM categories WHERE name = '$category'") or die('query failed');
if(mysqli_num_rows($select_book_category) > 0){
echo '<script>alert("Book Category already added!")</script>';
}else {
$sql = "INSERT INTO categories (name) VALUES ('$category')";
$result = mysqli_query($con, $sql);
if($result){
echo '<script>alert("Book Category Added Successfully!");
window.location.href="admin_category.php";</script>';
} else {
echo '<script>alert("Book Category not added!")</script>';
}
}
}
include './templates/admin_header.php';
?>
<link rel="stylesheet" href="./css/admin_category_add.css">
<div class="main-container b-main">
<?php include './templates/admin_navigation.php' ?>
<div class="category-container">
<div class="category-list">
<h2>Our Book Categories</h2><br>
<ul>
<?php
$select_book_category = mysqli_query($con, "SELECT * FROM categories") or die('query failed');
if(mysqli_num_rows($select_book_category) > 0){
while($fetch_book_category = mysqli_fetch_assoc($select_book_category)){
?>
<li><i class="fas fa-book"></i><span><?php echo $fetch_book_category['name']; ?></span> -
<a href="admin_category.php?delete=<?php echo $fetch_book_category['id']; ?>" onclick="return confirm('Delete this book category?')">Delete</a>/
<a href="admin_category.php?update=<?php echo $fetch_book_category['id']; ?>">Edit</a></li>
<?php
}
} else {
echo '<p class="empty">No book categories found!</p>';
}
?>
</ul>
<?php
if(isset($_GET['update'])){
$id = $_GET['update'];
$update_query = mysqli_query($con, "SELECT * FROM categories WHERE id = '$id'") or die('query failed');
if(mysqli_num_rows($update_query) > 0){
while($fetch_update = mysqli_fetch_assoc($update_query)){
?>
<form action="" method="post" id="cat-change-form">
<label for="update_name">Edit Category Name :</label>
<div class="input-field cat-input-field">
<input type="hidden" name="update_id" value="<?php echo $fetch_update['id']; ?>">
<input type="text" name="update_name" id="update_name" placeholder="Title" value="<?php echo $fetch_update['name']; ?>" required>
</div>
<div class="card-btns">
<input type="submit" name="update" value="Update" class="btn update-btn">
<input type="reset" name="cancel" value="Close" id="close-cat-update" class="btn">
</div>
</form>
<?php
}
}
}else{
echo '<script>document.querySelector("#cat-change-form").style.display = "none";</script>';
}
?>
</div>
<div class="container">
<div class="form-container">
<h2>Add Book Category</h2>
<form action="" method="post">
<div class="input-field">
<i class="fas fa-book"></i>
<input type="text" name="category" id="category" placeholder="category" required>
</div>
<input type="submit" name="addCategory" value="Add Book Category" class="submit-btn btn">
</form>
</div>
</div>
</div>
</div>
<script>
document.querySelector('#close-cat-update').onclick = () => {
document.querySelector('#cat-change-form').style.display = 'none';
}
</script>
<?php
include './templates/footer.php';
?>