Bài tập PHP nâng cao phần 7

PHP

Library Management System (OOP, Advanced)

Semester: 1  Duration: 90 minutes  Total Marks: 20

Database Setup

  1. Create and use the database CREATE DATABASE library_oop_db; USE library_oop_db;
  2. Create the books table CREATE TABLE books ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL UNIQUE, author VARCHAR(150) NOT NULL, published_year INT CHECK (published_year > 0), copies INT CHECK (copies >= 1) );
  3. Create the members table CREATE TABLE members ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL UNIQUE, membership_date DATE DEFAULT (CURDATE()) );
  4. Create the loans table CREATE TABLE loans ( id INT AUTO_INCREMENT PRIMARY KEY, book_id INT NOT NULL, member_id INT NOT NULL, loan_date DATE DEFAULT (CURDATE()), return_date DATE, FOREIGN KEY (book_id) REFERENCES books(id) ON DELETE RESTRICT, FOREIGN KEY (member_id) REFERENCES members(id) ON DELETE RESTRICT );
  5. Insert sample data INSERT INTO books (title, author, published_year, copies) VALUES ('1984', 'George Orwell', 1949, 5), ('Clean Code', 'Robert C. Martin', 2008, 3), ('The Pragmatic Programmer', 'Andrew Hunt', 1999, 4); INSERT INTO members (name, email) VALUES ('Nguyen Van A', '[email protected]'), ('Tran Thi B', '[email protected]'); INSERT INTO loans (book_id, member_id) VALUES (1, 1), (3, 2);

Question 1: OOP Structure & Book Listing (5 marks)

  • (2 marks) Create an abstract class Model that handles database connection (using PDO) and is extended by Book, Member, and Loan classes.
  • (1 mark) Implement a Book class with methods for fetching, inserting, updating, and deleting books. Use constructor injection for database connection.
  • (1 mark) Display all books with their available copy count (subtract the number of books currently on loan), sorted by published_year (desc).
  • (1 mark) For each book row, provide “Edit”, “Delete”, and “View Loans” buttons/links.

Question 2: Add Book with OOP (5 marks)

  • (1.5 marks) In add_book.php, use the Book class to display an HTML form (title, author, published_year, copies) and handle submission.
  • (1 mark) Validate: title must be unique (use a method of Book), published_year > 0, copies >= 1.
  • (2 marks) If validation passes, use the Book class to insert the record and display success/failure message.
  • (0.5 mark) If error, show appropriate error messages using PHP exceptions or custom error handling method in the class.

Question 3: Edit Book & Exception Handling (5 marks)

  • (2 marks) In edit_book.php, use the Book class to fetch book by id, pre-fill the form, and update the record.
  • (0.5 mark) Validate updated title is unique (except current record), published_year > 0, copies >= 1 (use class methods).
  • (1 mark) Implement custom exception classes (ValidationException, NotFoundException). Throw and catch these exceptions where appropriate (e.g., invalid input, book not found).
  • (1.5 marks) Only update if all validations pass, else show specific error messages.

Question 4: Deleting Books, Preventing Deletion on Loan (5 marks)

  • (1 mark) Add a “Delete” button using a POST request for security, and confirm via JavaScript.
  • (1 mark) Before deleting, Book class must check via Loan class if the book is on loan; if so, throw an exception and alert “Cannot delete because the book is currently on loan!”.
  • (2 marks) If deletable, delete the book and all related returned loans (with return_date set); prevent deleting books currently loaned out.
  • (1 mark) Handle and display all exceptions or errors user-friendly.

Bonus (Optional, +1 mark): Implement Inheritance

  • Implement a class Person as base class for Member (can be abstract or concrete), to demonstrate OOP inheritance. Add at least one method or property that is shared.

Requirement: All PHP must be object-oriented, using classes for business logic and database operations. Code must not use inline SQL in page files—use methods from respective classes.

Ảnh Nguyễn Hữu Huy

Tác giả

Giảng viên Khoa Công nghệ Thông tin, Cao đẳng FPT Polytechnic

Thầy Huy dạy lập trình tại Cao đẳng FPT Polytechnic, chia sẻ kiến thức về Android, Unity/C#, AI và lập trình backend qua blog dotplays và kênh YouTube Thầy Huy One Champ. Tác giả các ứng dụng dotsave, getvidfb và taptik.