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 = '