Could not connect to Google Books API.'; } } elseif (empty($search_query)) { $message = '
Please enter a search term.
'; } else { $message = '
Google Books API key is not configured. Please ask the administrator to set it up.
'; } } // Handle Add Book from Google Books if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_google_book'])) { $title = trim($_POST['title']); $author_name = trim($_POST['author']); $google_books_id = trim($_POST['google_books_id']); $description = trim($_POST['description']); $cover_image_url = trim($_POST['cover_image_url']); if (!empty($title) && !empty($author_name)) { try { // Check if book with this Google ID already exists $stmt = db()->prepare("SELECT id FROM books WHERE google_books_id = :google_books_id"); $stmt->execute(['google_books_id' => $google_books_id]); $book = $stmt->fetch(); if ($book) { $book_id = $book['id']; } else { // Insert new book $stmt = db()->prepare( "INSERT INTO books (title, author_name, description, cover_image_url, google_books_id, added_by_user_id) VALUES (:title, :author_name, :description, :cover_image_url, :google_books_id, :user_id)" ); $stmt->execute([ 'title' => $title, 'author_name' => $author_name, 'description' => $description, 'cover_image_url' => $cover_image_url, 'google_books_id' => $google_books_id, '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 = '
This book is already in your library.
'; } 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 = '
Book added to your library! View your library.
'; } } catch (PDOException $e) { $message = '
Database Error: ' . $e->getMessage() . '
'; } } else { $message = '
Title and Author are required.
'; } } ?>

Add Book from Google

Search for a book to add it to your library automatically.

Search Google Books
Search Results
Cover for <?php echo htmlspecialchars($title); ?>

by

...