SQL: Delete: Free PHP & MySQL Tutorial
Master MySQL DELETE Operations in PHP Applications
Always include the WHERE clause in DELETE statements. Without it, you'll delete every record in the table, which can be catastrophic for your database.
Learning Objectives
DELETE Statement Mastery
Learn the proper syntax and structure for SQL DELETE operations. Understand how to safely target specific records for deletion.
Database Record Management
Practice removing unwanted data from MySQL databases. Build confidence in database maintenance operations.
URL Parameter Handling
Master passing ID variables through URLs for dynamic record operations. Connect user interface actions to database changes.
Exercise Workflow
Display User List
View all records in the users table with delete links for each entry
Confirmation Page
Navigate to deleteConfirm.php to verify the deletion request before proceeding
Execute Deletion
Process the actual DELETE SQL statement and remove the record from the database
Return to List
Redirect back to the user list to confirm the record has been successfully deleted
DELETE FROM users WHERE id = 231
Safe vs Unsafe DELETE Operations
| Feature | Safe DELETE | Unsafe DELETE |
|---|---|---|
| Syntax | DELETE FROM users WHERE id = 231 | DELETE FROM users |
| Records Affected | Single targeted record | ALL records in table |
| Risk Level | Low - Controlled deletion | CRITICAL - Data loss |
| Reversibility | Manageable impact | Catastrophic - requires backup restore |
URL Parameter Implementation Steps
Creates the URL structure to pass user ID between pages
Dynamically inserts the actual user ID value into the URL
Confirms that user IDs are being correctly passed between pages
Ensures the confirmation page passes the ID to the deletion script
Using bind_param('i', $_GET['id']) prevents SQL injection attacks by treating the ID as an integer parameter rather than direct string concatenation.
Code Development Sequence
Modify userList.php
Add URL parameters to delete links around line 49
Update deleteConfirm.php
Pass user ID to deleteUser.php link around line 14
Complete deleteUser.php
Add SQL query, parameter binding, and redirect logic
Test Complete Flow
Verify deletion works end-to-end in browser
Key Takeaways