Easy Tutorial
❮ Pdo Rollback Func Curl_Exec ❯

PHP MySQL Update


The UPDATE statement is used to modify the data in a database table.


Updating Data in a Database

The UPDATE statement is used to update existing records in a database table.

Syntax

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

Note: Be aware of the WHERE clause in the UPDATE syntax. The WHERE clause specifies which records need to be updated. If you omit the WHERE clause, all records will be updated!

To learn more about SQL, visit our SQL Tutorial.

To execute the above statement in PHP, we must use the mysqli_query() function. This function is used to send a query or command to a MySQL connection.

Example

In a previous section of this tutorial, we created a table named "Persons" as shown below:

FirstName LastName Age
Peter Griffin 35
Glenn Quagmire 33

The following example updates some data in the "Persons" table:

Example

<?php
$con=mysqli_connect("localhost","username","password","database");
// Check connection
if (mysqli_connect_errno())
{
        echo "Failed to connect: " . 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 will look like this:

FirstName LastName Age
Peter Griffin 36
Glenn Quagmire 33
❮ Pdo Rollback Func Curl_Exec ❯