Module 9: CRUD Application
Module Overview
This module builds a full CRUD (Create, Read, Update, Delete) application using HTML, CSS, JavaScript, PHP, and MySQL. Students will connect all previous modules into a working web application.
Prerequisites: Modules 1-8 (HTML, CSS, JavaScript, PHP, MySQL)
Estimated Duration: 6-8 hours
Tools: XAMPP, VS Code, Browser, phpMyAdmin
Lesson 1: Database Schema and Connection
Duration: 60 minutes
Learning Objectives
- Design a complete database schema for a task management app
- Create tables with proper relationships
- Write a reusable database connection file in PHP
- Test the connection and handle errors
- Understand PDO vs mysqli
Instructor Notes
A database connection file is the backbone of any PHP/MySQL app. PDO is recommended for its security features (prepared statements) and support for multiple databases. Students should verify AI-generated connection code by testing connection failure scenarios.
Presentation Outline
- Application overview (10 min)
- Database schema design (15 min)
- Creating tables (10 min)
- PHP database connection with PDO (20 min)
Demonstration
In phpMyAdmin, create the task_db database and tables. Create config/database.php with PDO connection. Test the connection by running a simple query and displaying the result.
AI Prompts
- "Generate a MySQL database schema for a task management app with users and tasks tables"
- "Create a PHP PDO database connection file with error handling and best practices"
- "Write PHP code to test database connection and display connection status"
Starter Code
Final Code
Exercises
- Create an additional
tags table and a task_tags pivot table for many-to-many relationships.
- Modify the Database class to accept configuration from an external file.
- Add a method to the Database class that returns the last inserted ID.
Homework
- Draw an ER diagram (Entity-Relationship) for the task management app. Label primary keys, foreign keys, and relationships.
- Research PDO prepared statements. Write a summary explaining why they prevent SQL injection.
Quiz
- What does PDO stand for?
A) PHP Data Object
B) Python Database Object
C) PHP Database Orientation
D) Program Data Output
Answer: A
- Which PDO method executes a prepared statement?
A) execute()
B) run()
C) query()
D) Both A and B
Answer: D
- What does
PDO::ERRMODE_EXCEPTION do?
A) Logs errors silently
B) Throws exceptions on errors
C) Ignores all errors
D) Returns error codes
Answer: B
- Which SQL clause defines a foreign key?
A) FOREIGN KEY
B) REFERENCES
C) CONSTRAINT
D) All of the above
Answer: D
- What is the purpose of ON DELETE CASCADE?
A) Prevents deletion
B) Deletes child rows when parent is deleted
C) Archives deleted rows
D) Logs deletions
Answer: B
Lesson 2: Create and Read Operations
Duration: 60 minutes
Learning Objectives
- Build a task creation form
- Insert data into the database using PDO prepared statements
- Retrieve and display tasks from the database
- Implement basic search and filtering
- Handle form validation errors
Instructor Notes
Create and Read are the most used CRUD operations. Prepared statements are essential for security. Students should verify AI-generated INSERT/SELECT queries by checking the actual data in phpMyAdmin. Always validate input before inserting.
Presentation Outline
- Task creation form (15 min)
- INSERT with prepared statements (15 min)
- SELECT and display tasks (15 min)
- Search and filtering (10 min)
Demonstration
Create create.php with a task creation form. Create index.php to display all tasks with filtering by status. Show how data flows from form to database to display.
AI Prompts
- "Generate PHP PDO prepared statement examples for INSERT and SELECT operations"
- "Create a task management CRUD app with HTML forms and PHP processing"
- "Write PHP validation and error handling for a task creation form"
Starter Code
Final Code
Exercises
- Add a category dropdown to the task creation form.
- Add pagination to the task list (show 5 tasks per page).
- Add a "My Tasks" filter that shows only tasks assigned to the current user.
Homework
- Build a task creation form with client-side validation using JavaScript before form submission.
- Add a feature to export tasks to a CSV file using PHP.
Quiz
- Which PHP method executes a prepared statement?
A) query()
B) execute()
C) run()
D) Both B and C
Answer: D
- What does
htmlspecialchars() prevent?
A) SQL injection
B) XSS attacks
C) CSRF attacks
D) File inclusion
Answer: B
- Which HTTP method is typically used for form submissions that modify data?
A) GET
B) POST
C) PUT
D) DELETE
Answer: B
- What does
$_POST['field'] ?? '' do?
A) Returns an error if field is missing
B) Returns the field value or empty string if not set
C) Deletes the field
D) Sets the field to empty string
Answer: B
- Which SQL clause sorts results?
A) SORT BY
B) ORDER BY
C) ARRANGE BY
D) GROUP BY
Answer: B
Lesson 3: Update and Delete Operations
Duration: 60 minutes
Learning Objectives
- Build an edit form pre-filled with existing data
- Update database records using prepared statements
- Implement delete with confirmation
- Handle cascading deletes
- Add success/error messaging
Instructor Notes
Update and Delete operations require extra caution. Always confirm the record exists before updating. Use confirmation dialogs for deletes. Verify AI-generated update/delete queries by checking the affected row count.
Presentation Outline
- Update workflow (15 min)
- Edit form with pre-filled data (15 min)
- Delete with confirmation (15 min)
- Success and error messaging (10 min)
Demonstration
Create update.php that fetches a task by ID and pre-fills the form. On submission, update the task in the database. Create delete.php that confirms deletion and removes the task.
AI Prompts
- "Generate PHP CRUD update and delete operations with confirmation dialogs"
- "Create a PHP task edit form with pre-filled data and validation"
- "Write PHP code for soft delete vs hard delete with examples"
Starter Code
Final Code
Exercises
- Add a "soft delete" feature: add an
is_deleted column and filter deleted tasks from the main list.
- Add bulk delete functionality: allow selecting multiple tasks and deleting them at once.
- Add an "archive" feature that moves old completed tasks to an archive table.
Homework
- Add an audit log table that records all create, update, and delete operations with timestamps.
- Research CSRF protection. Implement a CSRF token in the task creation form.
Quiz
- Which HTTP method should be used for updates in RESTful APIs?
A) GET
B) POST
C) PUT
D) DELETE
Answer: C
- What does
$_SERVER['REQUEST_METHOD'] return for a form submission?
A) "FORM"
B) "POST" or "GET"
C) "SUBMIT"
D) "SEND"
Answer: B
- Which PHP function redirects to another page?
A) redirect()
B) header("Location: ...")
C) go_to()
D) navigate()
Answer: B
- Why should you use
exit() after a redirect?
A) It is required by PHP
B) To prevent the rest of the page from executing
C) To close the database connection
D) It is not necessary
Answer: B
- What is the recommended way to confirm a delete action?
A) Auto-delete without confirmation
B) JavaScript confirm() dialog
C) Ask the user to type "DELETE"
D) Both B and C
Answer: D
الوحدة 9: تطبيق CRUD
الدورة: البرمجة بأدوات الذكاء الاصطناعي المجانية — بناء مواقع وتطبيقات حقيقية | الجمهور: أعمار 15–25، مبتدئون | هدف الوحدة: بناء تطبيق CRUD كامل (إنشاء، قراءة، تحديث، حذف) باستخدام HTML, CSS, JavaScript, PHP, و MySQL.
المبدأ الأساسي: تحقق دائماً من الكود المولد بالذكاء الاصطناعي. تشغيل الاستعلامات، واختبار كل مسار، قبل الثقة بالنتيجة.
الدرس 1: مخطط قاعدة البيانات والاتصال
المدة: 60 دقيقة
ملخّص: تصميم مخطط قاعدة البيانات لتطبيق إدارة مهام، وإنشاء جداول users, tasks, categories مع foreign keys. كتابة ملف اتصال PBO في PHP.
الدرس 2: عمليات الإنشاء والقراءة (Create and Read)
المدة: 60 دقيقة
ملخّص: بناء نموذج إنشاء مهام، إدخال البيانات باستخدام prepared statements، وعرض المهام مع بحث وفلترة.
الدرس 3: عمليات التحديث والحذف (Update and Delete)
المدة: 60 دقيقة
ملخّص: بناء نموذج تعديل مُعبّأ مسبقاً، تحديث السجلات، تنفيذ الحذف مع تأكيد، وإضافة رسائل نجاح/خطأ.