Thuta Learning
AdvancedProgrammingbeginner

UPDATE Data

Relax. We'll talk through this in plain words — no textbook voice.

An UPDATE query is used to modify a record that already exists in the database. It's used for things like profile updates, order status changes, marking messages as read, or editing product prices. Forgetting the WHERE condition in an UPDATE is risky—it can end up updating the entire table.

php
<?php
$id = 1;
$newMessage = "I am learning PHP with practical examples.";

$stmt = $conn->prepare("UPDATE contacts SET message = ? WHERE id = ?");
$stmt->bind_param("si", $newMessage, $id);

if ($stmt->execute()) {
  echo "Message updated.";
} else {
  echo "Update failed.";
}

$stmt->close();
?>
You should see
Message updated.

Easy traps

  • Running UPDATE without a WHERE clause can modify every row in the table, which can be a huge disaster in production.