PHP CHAPTER 41

MySQL Update Data

UPDATE changes existing values in database records.

📘

What is it?

UPDATE changes existing values in database records.

💡

Why do we use it?

Users and administrators need to edit existing information.

⚙️

How does it work?

UPDATE changes selected columns for rows matching the WHERE condition.

🧩

Syntax

PHP Syntax
UPDATE students SET name=?, email=? WHERE id=?;
💻

Example

PHP Example
<?php

$conn = new mysqli("localhost", "root", "", "aifa_php_lab");

$id = 1;
$name = "Aman Kumar";
$email = "aman@example.com";

$stmt = $conn->prepare("UPDATE students SET name=?, email=? WHERE id=?");
$stmt->bind_param("ssi", $name, $email, $id);
$stmt->execute();

echo "Student Updated";

?>
🔍

Example Explained

The statement changes name and email for the student whose ID matches $id.
🚀

More Examples

Create an Edit Student form that loads existing data before updating it.

⚠️ Important Rules

Always identify the correct record with WHERE.

❌ Common Mistakes

Forgetting WHERE and updating every row.

🌍 Real-World Use

Profile editing, student management and admin dashboards.

📝 Practice Exercise

Build an Edit Student form connected to MySQL.

💻 Try It Yourself

Write and run this PHP lesson inside the AIFA Coding Lab.

Open Coding Lab →