← Back to Chapters

PHP MySQL CRUD – Update Data

? PHP MySQL CRUD – Update Data

? Quick Overview

The Update operation in CRUD is used to modify existing records in a MySQL database using PHP with MySQLi or PDO.

? Key Concepts

  • CRUD operations
  • UPDATE SQL statement
  • WHERE clause importance
  • Prepared statements

? Syntax / Theory

? View Code Example
// SQL UPDATE syntax
UPDATE table_name
SET column='value'
WHERE condition;

? Code Example

? View Code Example
// Update user email using PHP MySQLi
<?php
$conn=new mysqli("localhost","username","password","myDB");

if($conn->connect_error){
die("Connection failed");
}

$sql="UPDATE users SET email='john.doe@newdomain.com' WHERE id=1";
$conn->query($sql);

$conn->close();
?>

? Live Output / Explanation

Updates the selected user record and confirms the update if successful.

? Use Cases

  • User profile updates
  • Admin dashboards
  • Bulk record updates

✅ Tips & Best Practices

  • Always use WHERE
  • Validate input
  • Use prepared statements

? Try It Yourself

  • Update multiple columns
  • Rewrite using PDO
  • Update using form input