108 lines
4.2 KiB
PHP
108 lines
4.2 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION["user_id"])) {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
require_once 'header.php';
|
|
|
|
$user_id = $_SESSION["user_id"];
|
|
$message = '';
|
|
|
|
// Handle Add Book Form Submission
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_book'])) {
|
|
$title = trim($_POST['title']);
|
|
$author_name = trim($_POST['author']);
|
|
|
|
if (!empty($title) && !empty($author_name)) {
|
|
try {
|
|
// Check if the book already exists
|
|
$stmt = db()->prepare("SELECT id FROM books WHERE title = :title AND author_name = :author_name");
|
|
$stmt->execute(['title' => $title, 'author_name' => $author_name]);
|
|
$book = $stmt->fetch();
|
|
|
|
if ($book) {
|
|
$book_id = $book['id'];
|
|
} else {
|
|
// Insert new book
|
|
$stmt = db()->prepare("INSERT INTO books (title, author_name, added_by_user_id) VALUES (:title, :author_name, :user_id)");
|
|
$stmt->execute(['title' => $title, 'author_name' => $author_name, 'user_id' => $user_id]);
|
|
$book_id = db()->lastInsertId();
|
|
}
|
|
|
|
// Check if the book is already in the user's library
|
|
$stmt = db()->prepare("SELECT * FROM user_libraries WHERE user_id = :user_id AND book_id = :book_id");
|
|
$stmt->execute(['user_id' => $user_id, 'book_id' => $book_id]);
|
|
if ($stmt->fetch()) {
|
|
$message = '<div class="alert alert-warning">This book is already in your library.</div>';
|
|
} else {
|
|
// Add book to user's library
|
|
$stmt = db()->prepare("INSERT INTO user_libraries (user_id, book_id) VALUES (:user_id, :book_id)");
|
|
$stmt->execute(['user_id' => $user_id, 'book_id' => $book_id]);
|
|
$message = '<div class="alert alert-success">Book added to your library!</div>';
|
|
}
|
|
} catch (PDOException $e) {
|
|
$message = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
|
}
|
|
} else {
|
|
$message = '<div class="alert alert-danger">Please enter both title and author.</div>';
|
|
}
|
|
}
|
|
|
|
// Fetch user's books
|
|
$stmt = db()->prepare("SELECT b.id, b.title, b.author_name FROM books b JOIN user_libraries ul ON b.id = ul.book_id WHERE ul.user_id = :user_id ORDER BY b.title");
|
|
$stmt->execute(['user_id' => $user_id]);
|
|
$user_books = $stmt->fetchAll();
|
|
|
|
?>
|
|
|
|
<div class="container mt-5">
|
|
<h2>My Library</h2>
|
|
<p>Welcome, <?php echo htmlspecialchars($_SESSION["username"]); ?>!</p>
|
|
|
|
<?php echo $message; ?>
|
|
|
|
<div class="card mb-4">
|
|
<div class="card-header">
|
|
Add a New Book Manually
|
|
</div>
|
|
<div class="card-body">
|
|
<p>You can also <a href="add_book.php">search and add books automatically</a>.</p>
|
|
<form action="library.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>
|
|
<button type="submit" name="add_book" class="btn btn-primary">Add Book</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-header">
|
|
Your Books
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (count($user_books) > 0): ?>
|
|
<ul class="list-group">
|
|
<?php foreach ($user_books as $book): ?>
|
|
<li class="list-group-item">
|
|
<a href="book.php?id=<?php echo $book['id']; ?>"><?php echo htmlspecialchars($book['title']); ?></a>
|
|
by <?php echo htmlspecialchars($book['author_name']); ?>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
<?php else: ?>
|
|
<p>You haven't added any books yet.</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'footer.php'; ?>
|