-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_book.php
More file actions
306 lines (272 loc) · 8.98 KB
/
add_book.php
File metadata and controls
306 lines (272 loc) · 8.98 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
// Initialize variables for form fields
$itemName = $category = $description = $price = $photo = $quantity = "";
$itemNameErr = $categoryErr = $descriptionErr = $priceErr = $photoErr = $quantityErr = "";
// Form submission handling
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate item name
if (empty($_POST["item-name"])) {
$itemNameErr = "Item name is required";
} else {
$itemName = test_input($_POST["item-name"]);
}
// Validate category
if (empty($_POST["category"])) {
$categoryErr = "Category is required";
} else {
$category = test_input($_POST["category"]);
}
// Validate description
if (empty($_POST["description"])) {
$descriptionErr = "Description is required";
} else {
$description = test_input($_POST["description"]);
}
// Validate price
if (empty($_POST["price"])) {
$priceErr = "Price is required";
} else {
$price = test_input($_POST["price"]);
}
// Validate quantity
if (empty($_POST["quantity"])) {
$quantityErr = "Quantity is required";
} else {
$quantity = test_input($_POST["quantity"]);
}
// Validate photo upload
if (empty($_FILES["photo"]["name"])) {
$photoErr = "Photo is required";
} else {
// Handle photo upload
$targetDir = "uploads/";
$photoName = uniqid() . "_" . basename($_FILES["photo"]["name"]);
$targetFilePath = $targetDir . $photoName;
if (!file_exists($targetDir)) {
mkdir($targetDir, 0777, true);
}
if (move_uploaded_file($_FILES["photo"]["tmp_name"], $targetFilePath)) {
// Photo uploaded successfully
} else {
$photoErr = "Error uploading photo";
}
}
}
// Function to sanitize input data
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// Database connection details
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sem4";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Insert form data into database table
if (!empty($itemName) && !empty($category) && !empty($description) && !empty($price) && !empty($photoName) && !empty($quantity)) {
$sql = "INSERT INTO buy_items (item_name, category, description, price, photo, qty) VALUES ('$itemName', '$category', '$description', '$price', '$photoName', '$quantity')";
if ($conn->query($sql) === TRUE) {
echo "<script>alert('Book added successfully');</script>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
// Close database connection
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" href="image/logo.png">
<title>Add Books - Admin</title>
<style>
/* General Styles */
body {
font-family: 'Roboto', sans-serif;
background-color: #f0f2f5;
margin: 0;
padding: 0;
color: #333;
}
nav {
background-color: #2d2f34;
padding: 15px 20px;
display: flex;
justify-content: space-between;
align-items: center;
color: white;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
}
nav ul li {
margin-right: 20px;
}
nav ul li a {
color: white;
text-decoration: none;
font-weight: bold;
padding: 10px 15px;
border-radius: 5px;
transition: background-color 0.3s ease, color 0.3s ease;
}
nav ul li a:hover {
background-color: #444;
color: #fff;
text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}
/* Add Items Form */
section.add-items {
background-color: #fff;
padding: 30px;
margin: 30px auto;
width: 90%;
max-width: 700px;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
section.add-items h1 {
text-align: center;
font-size: 32px;
color: #333;
margin-bottom: 20px;
}
form {
display: flex;
flex-direction: column;
gap: 15px;
}
.form-group {
display: flex;
flex-direction: column;
}
label {
font-size: 16px;
font-weight: 600;
color: #555;
margin-bottom: 8px;
}
input[type="text"],
input[type="number"],
select,
textarea {
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
transition: border-color 0.3s ease, box-shadow 0.3s ease;
}
input[type="text"]:focus,
input[type="number"]:focus,
select:focus,
textarea:focus {
border-color: #4caf50;
box-shadow: 0 0 8px rgba(76, 175, 80, 0.2);
}
textarea {
resize: none;
}
.error {
color: #f44336;
font-size: 14px;
margin-top: 5px;
}
/* Buttons */
button {
background-color: #4CAF50;
color: white;
padding: 12px;
font-size: 16px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
width: 100%;
}
button:hover {
background-color: #45a049;
}
/* Responsive Design */
@media (max-width: 768px) {
nav ul li {
margin-right: 10px;
}
section.add-items {
padding: 20px;
width: 95%;
}
form {
gap: 10px;
}
}
</style>
</head>
<body>
<!-- Navbar -->
<nav>
Bookish-Admin
<ul>
<li><a href="admin_dashboard.php">Home</a></li>
<li><a href="buy_items.php">Books for Sale</a></li>
</ul>
</nav>
<!-- Add Items Form -->
<section class="add-items">
<h1>Add Books for Sale</h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="item-name">Book Name:</label>
<input type="text" id="item-name" name="item-name" value="<?php echo $itemName; ?>" required>
<span class="error"><?php echo $itemNameErr; ?></span>
</div>
<div class="form-group">
<label for="category">Category:</label>
<select id="category" name="category" required>
<option value="" <?php if(empty($category)) echo 'selected'; ?>>Select category</option>
<option value="New" <?php if($category == 'New') echo 'selected'; ?>>New</option>
<option value="Used" <?php if($category == 'Used') echo 'selected'; ?>>Used</option>
<!-- Add more options as needed -->
</select>
<span class="error"><?php echo $categoryErr; ?></span>
</div>
<div class="form-group">
<label for="description">Description:</label>
<textarea id="description" name="description" rows="4" required><?php echo $description; ?></textarea>
<span class="error"><?php echo $descriptionErr; ?></span>
</div>
<div class="form-group">
<label for="price">Price:</label>
<input type="number" id="price" name="price" value="<?php echo $price; ?>" required>
<span class="error"><?php echo $priceErr; ?></span>
</div>
<!-- New quantity input field -->
<div class="form-group">
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" value="<?php echo $quantity; ?>" required>
<span class="error"><?php echo $quantityErr; ?></span>
</div>
<!-- New photo upload field -->
<div class="form-group">
<label for="photo">Photo:</label>
<input type="file" id="photo" name="photo" accept="image/*" required>
<span class="error"><?php echo $photoErr; ?></span>
</div>
<button type="submit">Add Book</button>
</form>
</section>
</body>
</html>