-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_book.php
102 lines (86 loc) · 3.18 KB
/
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
<?php
// Start the session
session_start();
require 'db_config.php';
// Redirect if the user is not logged in
if (!isset($_SESSION['username'])) {
header("Location: login.html");
exit;
}
// Function to update the books.json file after adding a new book
function updateJsonFile($conn) {
$query = "SELECT * FROM books";
$result = $conn->query($query);
$books = [];
while ($row = $result->fetch_assoc()) {
$books[] = $row;
}
// Save the books array to books.json
file_put_contents('books.json', json_encode($books, JSON_PRETTY_PRINT));
}
// Handle form submission
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// Retrieve form data
$title = $_POST['title'] ?? '';
$author = $_POST['author'] ?? '';
$publication_year = $_POST['publication_year'] ?? 0;
$genre = $_POST['genre'] ?? '';
// Validate form data
if (empty($title) || empty($author) || empty($publication_year) || empty($genre)) {
echo "<script>alert('All fields are required.'); window.location.href='add_book.php';</script>";
exit;
}
// Insert data into the database
$stmt = $conn->prepare("INSERT INTO books (title, author, publication_year, genre) VALUES (?, ?, ?, ?)");
if (!$stmt) {
die("Prepare failed: " . $conn->error);
}
$stmt->bind_param("ssis", $title, $author, $publication_year, $genre);
if ($stmt->execute()) {
updateJsonFile($conn); // Update the JSON file after adding the book
echo "<script>alert('Book added successfully!'); window.location.href='add_book.php';</script>";
exit;
} else {
die("Error adding book: " . $stmt->error);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" type="image/x-icon" href="favicon\android-chrome-512x512.png">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Book</title>
<link rel="stylesheet" href="common.css">
<link rel="stylesheet" href="add_book.css">
</head>
<body>
<?php include 'navbar.php'; ?>
<main>
<h1>Add a New Book</h1>
<form action="add_book.php" method="POST">
<div class="form-group">
<label for="title">Title:</label>
<input type="text" id="title" name="title" required placeholder="Enter book title">
</div>
<div class="form-group">
<label for="author">Author:</label>
<input type="text" id="author" name="author" required placeholder="Enter author's name">
</div>
<div class="form-group">
<label for="publication_year">Publication Year:</label>
<input type="number" id="publication_year" name="publication_year" min="2000" required placeholder="Enter publication year (after 2000)">
</div>
<div class="form-group">
<label for="genre">Genre:</label>
<input type="text" id="genre" name="genre" required placeholder="Enter genre">
</div>
<button type="submit">Add Book</button>
</form>
</main>
<footer>
<p>© 2024 Library Management System</p>
</footer>
</body>
</html>