Topics Covered in This PHP & MySQL Tutorial:
The DELETE Statement, Deleting Rows from a Database, Passing ID Variables in a URL
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 Overview
This exercise demonstrates how to safely delete records from a MySQL database using PHP. You'll learn to implement a multi-step deletion process that includes user confirmation and proper security measures—essential skills for any web application that manages user data.
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 Syntax
Deleting database records follows a straightforward SQL pattern, but requires careful attention to security. To delete user 231 from the users table, you would execute:
DELETE FROM users
WHERE id = 231
The WHERE clause is absolutely critical here. Without it, you would delete every record in the table—a catastrophic error that has destroyed countless databases in production environments. Always double-check your WHERE conditions before executing DELETE statements.
In this exercise, we'll construct a secure, user-friendly deletion workflow consisting of three interconnected pages that guide users through the deletion process.
Open userList.php from the delete folder.
Navigate to the file in your browser:
- Mac: localhost:8888/phpclass/delete/userList.php
- Windows: localhost/phpclass/delete/userList.php
You'll see a comprehensive list of all records in the users table. Notice the last column contains Delete links that will redirect users to a confirmation page—this two-step process prevents accidental deletions and follows modern UX best practices.
Return to your code editor to examine the underlying structure.
Locate the Delete link around line 49. Currently it points to deleteConfirm.php without passing any identifying information:
<td><a href="deleteconfirm.php">Delete</a></td>Add the URL parameter structure as shown in bold:
<td><a href="deleteconfirm.php?id=">Delete</a></td>Now integrate the PHP code that dynamically inserts each user's unique ID. Add the bold code directly after id=:
<td><a href="deleteconfirm.php?id=<?php echo $id; ?>">Delete</a></td>Save your changes and test the updated functionality in your browser:
- Mac: localhost:8888/phpclass/delete/userList.php
- Windows: localhost/phpclass/delete/userList.php
Click several different Delete links and observe the behavior. You'll be directed to deleteConfirm.php with the specific user ID visible in the URL query string—this demonstrates how data flows between pages in web applications.
The deleteConfirm.php page serves as a crucial safety checkpoint, presenting users with clear options: proceed with deletion or return safely to the user list.
Switch back to your code editor to continue the implementation.
Open deleteConfirm.php from the delete folder.
Around line 14, modify the deleteUser.php link to pass along the user ID as shown in bold:
<p><a href="deleteuser.php?id=<?php echo $_GET['id']; ?>">Delete User</a></p>This code leverages PHP's superglobal
$_GETarray to retrieve the ID parameter from the previous page, maintaining data continuity throughout the deletion workflow.Save your changes and prepare for the final implementation step.
Open deleteUser.php from the delete folder.
The database connection and prepared statement framework are already established. Your task is to implement the core SQL deletion logic, bind the parameters securely, and handle the post-deletion user experience.
Locate the empty
$SQLvariable around line 5 and add the deletion query:$SQL = "DELETE FROM users WHERE id = ? ";This parameterized query uses a placeholder (?) instead of directly inserting the ID value, protecting against SQL injection attacks—a fundamental security practice in modern web development.
Find the //bind params here comment around line 12 and replace it with the parameter binding code:
$stmt->bind_param('i', $_GET['id']);The 'i' parameter specifies that PHP should expect an integer value, adding an additional layer of type safety to your database operations.
Locate the //go back to user list comment around line 19 and add the redirect functionality:
//go back to user list require_once('userList.php');This approach seamlessly returns users to the updated list, where they can immediately verify that the deletion was successful.
Save your completed implementation and test the full deletion workflow:
- Mac: localhost:8888/phpclass/delete/userList.php
- Windows: localhost/phpclass/delete/userList.php
Select a specific Delete link and take note of which user record you're targeting for deletion.
On the confirmation page, click Delete User and observe as the system removes the record and displays the updated user list, confirming the successful deletion.
Return to your code editor and close any open files—you've successfully implemented a complete database deletion system.
DELETE FROM users WHERE id = 231Safe 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