Updating Records via SQL Query in PHP
Let's now learn how to modify records.
This is done using the UPDATE command.
It has the following syntax:
<?php
$query = "UPDATE table SET field = value WHERE condition";
?>
Example
Let's change the age and salary of a user:
<?php
$query = "UPDATE users SET age=20, salary=800 WHERE id=1";
?>
Example
Set the salary to 400 and age to 24
for all users aged 23:
<?php
$query = "UPDATE users SET age=24, salary=300 WHERE age=23";
?>
Example
Without the WHERE command, updates will affect
the entire table. For example, set the salary to
400 and age to 24 for all users:
<?php
$query = "UPDATE users SET age=24, salary=300";
?>
Using the dump of the users table you created earlier,
bring it back to its original state.
Set the age to 35 for the user with id 4.
For everyone with a salary of 500, set it
to 700.
Set the age to 23 for employees with
id greater than 2 and
less than or equal to 5.