What is the UPDATE Operation?

UPDATE is the operation used to modify existing data in the database. It takes changes from a form and updates only the specific record without affecting other data.

UPDATE operations are used when you edit your profile, change your password, update a post, or modify any information you previously created.

SQL Query Used:

UPDATE students
SET name='Jane Doe', email='jane@example.com', class='B', status='active'
WHERE id=5;

Key Features of UPDATE:

  • Modifies existing records (doesn't delete or create new ones)
  • Uses WHERE clause to identify which record to update
  • All fields are re-validated before updating
  • Should confirm the record exists before updating
  • Usually requires user authentication

📋 How UPDATE Works - Step by Step

1ī¸âƒŖ Load Existing Data

READ the current student record from the database and display it in a form

2ī¸âƒŖ User Edits Fields

User changes any fields they want to modify in the form

3ī¸âƒŖ Server Validates New Data

PHP checks: Are all fields valid? Is the email format correct? Is there a valid status?

4ī¸âƒŖ Prepare SQL Statement

Create a prepared statement with a WHERE clause to identify which record to update

5ī¸âƒŖ Execute UPDATE Query

Run the SQL UPDATE command to modify the specific record in the database

6ī¸âƒŖ Confirm & Redirect

Show success message and redirect to the student list to show the changes

âš ī¸ This is a Demo Form

The form below shows how an UPDATE operation looks and feels (with current student data pre-filled). To actually update students in the database, you need to login to the admin panel.

🎨 Example UPDATE Form (With Pre-filled Data)

â„šī¸ Notice: The form shows existing student data (READ), user can edit it, then SUBMIT to UPDATE

Currently: John Doe (editable when updating)
Currently: john@example.com (editable when updating)
Currently: A (editable when updating)
Currently: Active (editable when updating)

âš ī¸ This form is disabled (read-only). Login as admin to edit students.

🔐 Login to Edit Students

📊 UPDATE vs CREATE

Aspect CREATE UPDATE
Purpose Add new record Modify existing record
SQL Used INSERT UPDATE ... WHERE
Pre-filled Data No, form is empty Yes, form shows current data
Result New ID is generated ID stays the same
Row Count Database grows Database size unchanged

🔐 Security Measures

WHERE Clause Importance

The WHERE clause identifies which specific record to update. Without it, ALL records would be updated!

// ✅ CORRECT: Updates only student with id=5
UPDATE students SET name='Jane' WHERE id=5;

// ❌ WRONG: Updates ALL students!
UPDATE students SET name='Jane';

Prepared Statements

We use prepared statements to prevent SQL injection even in UPDATE operations.

$stmt = $conn->prepare('UPDATE students SET name=?, email=?, class=?, status=? WHERE id=?');
$stmt->bind_param('ssssi', $name, $email, $class, $status, $id);
$stmt->execute();

📚 Continue Learning CRUD

Now that you understand the UPDATE operation, explore the other operations:

đŸ‘ī¸ READ Operation ➕ CREATE Operation đŸ—‘ī¸ DELETE Operation 🔐 Login to Try