PHP MySQL Update

« Previous Chapter Next Chapter »

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

Update Data In a Database

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


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

Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

To learn more about SQL, please visit our SQL tutorial.

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


Earlier in the tutorial we created a table named "Persons". Here is how it looks:

FirstName LastName Age
William Dharsan 35
Glenn Boopathi 33

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

$con = mysql_connect("localhost","William","password");
if (!$con)
  die('Could not connect: ' . mysql_error());

mysql_select_db("my_db", $con);

mysql_query("UPDATE Persons SET Age = '36'
WHERE FirstName = 'William' AND LastName = 'Dharsan'");


After the update, the "Persons" table will look like this:

FirstName LastName Age
William Dharsan 36
Glenn Boopathi 33

« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!