Introduction
In dynamic websites, data doesn’t just get inserted and displayed — it also needs to be updated and sometimes deleted.
For example, users may update their profile information, admins may edit product details, or old records may need to be removed from the database.
In this tutorial, we’ll learn how to update and delete records in MySQL using PHP with mysqli.
Prerequisites
Before proceeding:
- Database connection must be ready
- Database and table are already created
- Data should already exist in the table
- Example table:
users
| id | name |
|---|
SQL UPDATE Query
UPDATE users SET name='John Doe' WHERE id=1;
This query updates the record with id = 1.
Update Data Using PHP
Example
<?php
include "db.php";
$id = 1;
$name = "John Doe";
$email = "john@newmail.com";
$sql = "UPDATE users SET name='$name', email='$email' WHERE id=$id";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record";
}
?>
Explanation
UPDATEmodifies existing dataWHEREis important to target a specific record- Without
WHERE, all records may get updated mysqli_query()executes the SQL query
Update Data Using HTML Form
HTML Form
<form method="post">
<input type="text" name="name" placeholder="Name">
<input type="email" name="email" placeholder="Email">
<button type="submit">Update</button>
</form>
PHP Code
<?php
include "db.php";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$sql = "UPDATE users SET name='$name', email='$email' WHERE id=1";
mysqli_query($conn, $sql);
}
?>
SQL DELETE Query
DELETE FROM users WHERE id=1;
This query deletes the selected record permanently.
Delete Data Using PHP
Example
<?php
include "db.php";
$id = 1;
$sql = "DELETE FROM users WHERE id=$id";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record";
}
?>
Delete Record Using Link (Basic)
<a href="delete.php?id=1">Delete</a>
<?php
include "db.php";
$id = $_GET['id'];
mysqli_query($conn, "DELETE FROM users WHERE id=$id");
?>
⚠️ This is not secure and is shown only for learning.
Check Record Exists Before Update or Delete (Good Practice)
Before updating or deleting a record, it is a good practice to check whether the record actually exists.
Example logic:
$result = mysqli_query($conn, "SELECT id FROM users WHERE id = $id");if (mysqli_num_rows($result) > 0) {
// record exists, you can update or delete
} else {
echo "Record not found";
}
This avoids confusion and helps you display meaningful messages like “User not found” instead of silently failing.
Important Security Warning ⚠️
- Never trust user input directly
- Always validate IDs
- Use prepared statements
- Confirm delete actions
We will secure this properly in the next lesson.
When to Use UPDATE vs DELETE (Real-World Examples)
Understanding when to update data and when to delete it is important in real projects.
Use UPDATE when:
- A user changes their name or email
- A product price is edited
- A profile photo is updated
- An admin edits post content
Use DELETE when:
- A user account is permanently removed
- Spam or fake records need to be deleted
- Test data should be cleared from the database
- Old or irrelevant records must be removed
In most applications, DELETE operations are restricted to admins to avoid accidental data loss.
Common Mistakes to Avoid
- Forgetting WHERE clause (can delete all data!)
- Using wrong column names
- Not checking if the record exists
- Updating wrong records
- Not validating user input
- Allowing deletion without confirmation
- Not checking query success
Practical Task
Try this on your local server:
- Update the email of a user using their ID
- Change only the name of one record
- Delete one test record from the table
- Try updating a record that does not exist
- Observe what happens when
WHEREis removed (don’t run on real data)
Summary
- UPDATE modifies existing data
- DELETE removes records permanently
- WHERE clause is critical
- PHP executes SQL queries using
mysqli - Security must be handled carefully
In the next tutorial, we’ll learn about Prepared Statements in PHP (Prevent SQL Injection).
