35354-vm/add-book.php
Flatlogic Bot 2332ec6934 1.0
2025-10-30 14:24:12 +00:00

99 lines
3.4 KiB
PHP

<?php
require_once __DIR__ . '/includes/auth.php';
require_role('Librarian');
require_once __DIR__ . '/db/config.php';
$errors = [];
$success = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$title = $_POST['title'] ?? '';
$author = $_POST['author'] ?? '';
$isbn = $_POST['isbn'] ?? '';
$description = $_POST['description'] ?? '';
if (empty($title)) {
$errors[] = 'Title is required';
}
if (empty($author)) {
$errors[] = 'Author is required';
}
if (empty($isbn)) {
$errors[] = 'ISBN is required';
}
if (empty($errors)) {
try {
$pdo = db();
$qr_code_hash = md5($isbn);
$stmt = $pdo->prepare(
"INSERT INTO books (title, author, isbn, description, qr_code_hash) VALUES (:title, :author, :isbn, :description, :qr_code_hash)"
);
if ($stmt->execute([
':title' => $title,
':author' => $author,
':isbn' => $isbn,
':description' => $description,
':qr_code_hash' => $qr_code_hash
])) {
$success = "Book added successfully!";
} else {
$errors[] = "Failed to add book.";
}
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
require_once __DIR__ . '/includes/header.php';
?>
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h2>Add a New Book</h2>
</div>
<div class="card-body">
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?>
<p><?php echo $error; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success">
<p><?php echo $success; ?></p>
</div>
<?php endif; ?>
<form action="add-book.php" method="POST">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="mb-3">
<label for="author" class="form-label">Author</label>
<input type="text" class="form-control" id="author" name="author" required>
</div>
<div class="mb-3">
<label for="isbn" class="form-label">ISBN</label>
<input type="text" class="form-control" id="isbn" name="isbn" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">Add Book</button>
</form>
</div>
</div>
</div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>