May 11, 2021 PHP
The UPDATE statement is used to modify the data in the database table.
The UPDATE statement is used to update records that already exist in the database table.
Note: Note the
WHERE clause in the
UPDATE syntax.
T
he WHERE clause specifies which records need to be updated.
If you want to save the WHERE clause, all records will be updated!
To learn more about SQL, visit our SQL tutorial.
In order for PHP to execute the statement above, we must use mysqli_query () function. This function is used to send queries or commands to MySQL connections.
In the previous sections of this tutorial, we created a table called "Peoples" that looks like this:
FirstName | LastName | Age |
---|---|---|
Peter | Griffin | 35 |
Glenn | Quagmire | 33 |
The following example updates some of the data from the "Peoples" table:
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE Persons SET Age=36
WHERE FirstName='Peter' AND LastName='Griffin'");
mysqli_close($con);
?>
After this update, the Persons table looks like this:
FirstName | LastName | Age |
---|---|---|
Peter | Griffin | 36 |
Glenn | Quagmire | 33 |