-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_book.php
183 lines (172 loc) · 4.95 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
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
<?php
// Database connection
$host = 'localhost';
$db = 'personal_library';
$user = 'root';
$pass = 'password';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'] ?? '';
$author = $_POST['author'] ?? '';
$isbn = $_POST['isbn'] ?? '';
$status = $_POST['status'] ?? 'unread';
// Simple validation
if (empty($title) || empty($author)) {
$message = 'Title and Author are required.';
} else {
$cover_image = 'https://via.placeholder.com/100x150.png?text=No+Cover';
$stmt = $pdo->prepare("INSERT INTO books (title, author, isbn, status, cover_image) VALUES (?, ?, ?, ?, ?)");
if ($stmt->execute([$title, $author, $isbn, $status, $cover_image])) {
$message = 'Book added successfully!';
} else {
$message = 'Error adding book.';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add New Book - Personal Library Management System</title>
<style>
/* Copy styles from index.php and add these: */
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 1000px;
margin: 0 auto;
padding: 20px;
}
header {
background-color: #f4f4f4;
padding: 1rem;
text-align: center;
}
nav {
display: flex;
justify-content: center;
padding: 1rem 0;
}
nav a {
color: #333;
padding: 0.5rem 1rem;
text-decoration: none;
}
nav a:hover {
background-color: #ddd;
}
.dashboard {
display: flex;
justify-content: space-around;
background-color: #e9e9e9;
padding: 1rem;
margin-bottom: 1rem;
}
.stat {
text-align: center;
}
.book-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 1rem;
}
.book {
border: 1px solid #ddd;
padding: 1rem;
text-align: center;
}
.book img {
max-width: 100px;
height: auto;
}
footer {
text-align: center;
margin-top: 2rem;
padding: 1rem;
background-color: #f4f4f4;
}
form {
max-width: 500px;
margin: 0 auto;
}
label {
display: block;
margin-top: 1rem;
}
input[type="text"],
input[type="number"],
select {
width: 100%;
padding: 0.5rem;
margin-top: 0.5rem;
}
button {
display: block;
width: 100%;
padding: 0.5rem;
margin-top: 1rem;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.message {
text-align: center;
margin-top: 1rem;
padding: 0.5rem;
background-color: #f4f4f4;
}
</style>
</head>
<body>
<header>
<h1>Personal Library Management System</h1>
</header>
<nav>
<a href="index.php">Home</a>
<a href="add_book.php">Add Book</a>
<a href="search.php">Search</a>
</nav>
<main>
<h2>Add New Book</h2>
<?php if ($message): ?>
<p class="message"><?= htmlspecialchars($message) ?></p>
<?php endif; ?>
<form method="POST">
<label for="title">Title:</label>
<input type="text" id="title" name="title" required>
<label for="author">Author:</label>
<input type="text" id="author" name="author" required>
<label for="isbn">ISBN:</label>
<input type="text" id="isbn" name="isbn">
<label for="status">Status:</label>
<select id="status" name="status">
<option value="unread">Unread</option>
<option value="read">Read</option>
</select>
<button type="submit">Add Book</button>
</form>
</main>
<footer>
<p>© 2024 Personal Library Management System. All rights reserved.</p>
</footer>
</body>
</html>