[75f74d9] | 1 | <?php
|
---|
| 2 | require '../connect.php';
|
---|
| 3 |
|
---|
| 4 | header('Content-Type: application/json');
|
---|
| 5 |
|
---|
| 6 | try {
|
---|
| 7 | // Ensure Book ID is provided
|
---|
| 8 | if (!isset($_GET['id']) || empty($_GET['id'])) {
|
---|
| 9 | echo json_encode(['error' => 'Book ID is required']);
|
---|
| 10 | exit();
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | $bookId = $_GET['id'];
|
---|
| 14 |
|
---|
| 15 | // Fetch basic book details
|
---|
| 16 | $query = "SELECT * FROM book WHERE bookid = :bookId";
|
---|
| 17 | $stmt = $conn->prepare($query);
|
---|
| 18 | $stmt->execute([':bookId' => $bookId]);
|
---|
| 19 |
|
---|
| 20 | $book = $stmt->fetch(PDO::FETCH_ASSOC);
|
---|
| 21 |
|
---|
| 22 | if (!$book) {
|
---|
| 23 | echo json_encode(['error' => 'Book not found']);
|
---|
| 24 | exit();
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | // Fetch additional book details
|
---|
| 28 | $sql = "
|
---|
| 29 | SELECT
|
---|
| 30 | b.BookID,
|
---|
| 31 | b.ISBN,
|
---|
| 32 | b.Title,
|
---|
| 33 | b.Genre,
|
---|
| 34 | b.PublishedYear,
|
---|
| 35 | b.Description,
|
---|
| 36 | b.CoverImage,
|
---|
| 37 | b.TotalCopies,
|
---|
| 38 | bd.Format,
|
---|
| 39 | bd.Language,
|
---|
| 40 | bd.Publisher,
|
---|
| 41 | bd.Pages
|
---|
| 42 | FROM Book b
|
---|
| 43 | LEFT JOIN Book_Details bd ON b.BookID = bd.BookID
|
---|
| 44 | WHERE b.BookID = :bookId
|
---|
| 45 | ";
|
---|
| 46 | $stmt = $conn->prepare($sql);
|
---|
| 47 | $stmt->bindParam(':bookId', $bookId, PDO::PARAM_INT);
|
---|
| 48 | $stmt->execute();
|
---|
| 49 |
|
---|
| 50 | if ($stmt->rowCount() === 0) {
|
---|
| 51 | echo json_encode(['error' => 'Book details not found']);
|
---|
| 52 | exit();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | $bookDetails = $stmt->fetch(PDO::FETCH_ASSOC);
|
---|
| 56 |
|
---|
| 57 | // Merge book details into the original book data
|
---|
| 58 | $book = array_merge($book, $bookDetails);
|
---|
| 59 |
|
---|
| 60 | // Fetch authors associated with the book
|
---|
| 61 | $sqlAuthors = "
|
---|
| 62 | SELECT a.AuthorID, a.FirstName, a.LastName
|
---|
| 63 | FROM Book_Author ba
|
---|
| 64 | JOIN Author a ON ba.AuthorID = a.AuthorID
|
---|
| 65 | WHERE ba.BookID = :bookId
|
---|
| 66 | ";
|
---|
| 67 | $stmtAuthors = $conn->prepare($sqlAuthors);
|
---|
| 68 | $stmtAuthors->bindParam(':bookId', $bookId, PDO::PARAM_INT);
|
---|
| 69 | $stmtAuthors->execute();
|
---|
| 70 |
|
---|
| 71 | $authors = $stmtAuthors->fetchAll(PDO::FETCH_ASSOC); // Fetch all author details
|
---|
| 72 | $book['authors'] = $authors;
|
---|
| 73 |
|
---|
| 74 | // Fetch book copies with their condition
|
---|
| 75 | $sqlCopies = "
|
---|
| 76 | SELECT bc.CopyID, bc.Condition
|
---|
| 77 | FROM Book_Copies bc
|
---|
| 78 | WHERE bc.BookID = :bookId
|
---|
| 79 | ";
|
---|
| 80 | $stmtCopies = $conn->prepare($sqlCopies);
|
---|
| 81 | $stmtCopies->bindParam(':bookId', $bookId, PDO::PARAM_INT);
|
---|
| 82 | $stmtCopies->execute();
|
---|
| 83 |
|
---|
| 84 | $copies = $stmtCopies->fetchAll(PDO::FETCH_ASSOC);
|
---|
| 85 | $book['copies'] = $copies;
|
---|
| 86 |
|
---|
| 87 | echo json_encode($book);
|
---|
| 88 | } catch (PDOException $e) {
|
---|
| 89 | echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
|
---|
| 90 | exit();
|
---|
| 91 | }
|
---|
| 92 | ?>
|
---|