-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadmin_add_book.php
270 lines (222 loc) · 7.06 KB
/
admin_add_book.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
// if (!isset($_SESSION['username']) && !$_SESSION['login_success']) {
// header(" Location: login.php");
// exit();
// }
include './connection/config.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$title = trim($_POST['title']);
$author = trim($_POST['author']);
$genre = trim($_POST['genre']);
$pub_year = trim($_POST['pub_year']);
$isbn = $_POST['isbn'];
$publisher = trim($_POST['publisher']);
$price = trim($_POST['price']);
$stock = $_POST['stock'];
$bookfile_name = basename($_FILES["book_image"]["name"]);
if (!file_exists('bookspic')) {
mkdir('bookspic', 0777, true);
}
if (empty($title) || empty($author) || empty($genre) || empty($pub_year) || empty($isbn) || empty($publisher) || empty($price) || empty($bookfile_name) || empty($stock)) {
header("Location: admin_add_book?error=empty_fields");
exit();
}
$target_dir = "bookspic/";
$file_extension = strtolower(pathinfo($bookfile_name, PATHINFO_EXTENSION));
$new_book_img = uniqid() . '.' . $file_extension; // Generate unique filename
$target_file = $target_dir . $new_book_img;
// Check if image file is actual image
$check = getimagesize($_FILES["book_image"]["tmp_name"]);
if ($check === false) {
header("Location: admin_add_book.php?error=img_upl_err");
exit();
}
// Allow certain file formats
if ($file_extension != "jpg" && $file_extension != "png" && $file_extension != "jpeg" && $file_extension != "gif") {
header("Location: admin_add_book.php?error=not_suitable_img_format");
exit();
}
$sql = "SELECT id FROM books WHERE title=?";
if ($stmt = $mysqli->prepare($sql)) {
$stmt->bind_param("s", $title);
if ($stmt->execute()) {
$stmt->store_result();
if ($stmt->num_rows > 0) {
echo "add error";
header("Location: admin_add_book.php?error=books_already_in_store");
exit();
}
}
}
$stmt;
// inserting book in the database
// first we need to uplaod file
if (move_uploaded_file($_FILES["book_image"]["tmp_name"], $target_file)) {
if ($stmt = $mysqli->prepare("INSERT INTO books(title, author, genre, pub_year, isbn, publisher, price, stock, book_img ) VAlUES (?, ?, ?, ?, ?, ?, ?, ?,?)")) {
$stmt->bind_param("sssissdis", $title, $author, $genre, $pub_year, $isbn, $publisher, $price, $stock, $new_book_img);
if ($stmt->execute()) {
$stmt->store_result();
echo "book added into database";
header("Location: admin_add_book.php?add_book=success");
$stmt->close();
exit();
} else {
header("Location: admin_add_book.php?add_book=Book_add_fail");
$stmt->close();
exit();
}
}
}
}
// login error get
$error = '';
if (isset($_GET['error'])) {
switch ($_GET['error']) {
case 'img_upl_err':
$error = "Image Upload Error! Use Smaller size";
break;
case 'not_suitable_img_format':
$error = "Not Suitable Image format ";
break;
case 'books_already_in_store':
$error = "Alread in Store! Please Add new Book";
break;
case 'empty_fields':
$error = "All fields are required!";
break;
default:
$error = "";
break;
}
}
//msg when book added
if (isset($_GET['add_book'])) {
switch ($_GET['add_book']) {
case 'success':
$error = "Successfully Book Added";
break;
case 'Book_add_fail':
$error = "Error While Adding Book in Store";
break;
default:
$error = "";
break;
}
}
?>
<?php
$_SESSION['username'] = "Deepak";
$username = $_SESSION['username'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Boxicons -->
<link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
<!-- My CSS -->
<link rel="stylesheet" href="./css/admindashboard.css">
<link rel="stylesheet" href="./css/admin_add_book.css">
<title>Dashboard Admin: <?php echo $username ?> </title>
<style>
.error {
background: #4b49ac;
color: white;
margin: 8px;
padding: 7px;
border-radius: 10px;
}
</style>
</head>
<body>
<!-- SIDEBAR -->
<?php include 'admin_sidebar.php' ?>;
<!-- SIDEBAR -->
<!-- CONTENT -->
<section id="content">
<!-- NAVBAR -->
<nav>
<a href="#" class="nav-link">Admin Dashboard : Add Book</a>
<div class="nav-link-2">
<a href="admin_profile.php" class="profile">
<img src="img/people.png">
</a>
</div>
</nav>
<!-- NAVBAR -->
<!--MAIN CONTENT -->
<div class="addbook-container" id="main-content">
<h2>
<?php
if ($error) {
echo "<div class='error'> $error </div>";
}
?>
</h2>
<h1>Enter Book Details</h1>
<form action="admin_add_book.php" method="POST" enctype="multipart/form-data" class="form-group">
<div class="input-group">
<label for="title">Title:</label>
<input type="text" id="title" name="title" required>
</div>
<!-- Author -->
<div class="input-group">
<label for="author">Author:</label>
<input type="text" id="author" name="author" required>
</div>
<!-- Genre -->
<div class="input-group">
<label for="genre">Genre:</label>
<input type="text" id="genre" name="genre">
</div>
<!-- Publication Year -->
<div class="input-group">
<label for="pub_year">Publication Year:</label>
<input type="number" id="pub_year" name="pub_year" min="1000" max="9999">
</div>
<!-- ISBN -->
<div class="input-group">
<label for="isbn">ISBN:</label>
<input type="text" id="isbn" name="isbn" required>
</div>
<!-- Publisher -->
<div class="input-group">
<label for="publisher">Publisher:</label>
<input type="text" id="publisher" name="publisher">
</div>
<!-- Price -->
<div class="input-group">
<label for="price">Price:</label>
<input type="number" id="price" name="price" step="0.01" min="0">
</div>
<!-- Stock -->
<div class="input-group">
<label for="stock">Stock Quantity:</label>
<input type="number" id="stock" name="stock" min="0" step="1">
</div>
<!-- Image -->
<div class="input-group">
<label for="img">Book Cover Image:</label>
<input type="file" name="book_image" id="book_image" accept="bookspic/" required>
</div>
<!-- Submit Button -->
<button type="submit">Add Book</button>
</form>
</div>
</section>
<!-- CONTENT -->
<script>
const allSideMenu = document.querySelectorAll('#sidebar .side-menu.top li a');
allSideMenu.forEach(item => {
const li = item.parentElement;
item.addEventListener('click', function () {
allSideMenu.forEach(i => {
i.parentElement.classList.remove('active');
})
li.classList.add('active');
})
});
</script>
</body>
</html>