How to Update a Value in a MySQL Table Using PHP

PHP



Update John's State:












In this article, we show how to update a value in a MySQL table using PHP.

By updating, we mean we change a value in the MySQL table.

The general format of the MySQL query to update a MySQL table is shown below.



So this MySQL code above updates a table named table_name, setting a column name equal to a certain value where a certain column is equal to a value.

I'll give a practical example because this is kind of hard to understand.

An example is shown below.



In the example shown above, we update a table named Users_table and set the state column equal to FL where the name column equals to John. In simpler words, we want to update the user John's state to FL. Maybe he moved from NY to FL.

This setup is great anytime you need to update values.

We will show how to tie this in with PHP to update a value in a table.




So, first, we must connect to the database.

After we do this, we create a variable named $update. This $update variable is set equal to a mysql_query() function that has the parameter "UPDATE Users_Table SET state='FL' WHERE name='John'". This updates the user John's state to FL.

The UPDATE keyword is used followed by the table name. This is how the code knows which table in the database we want to update. After this, we use the SET keyword to set a certain column equal to a certain value where a certain column is equal to a certain value so that we know which row to update.

And this is how a value in a MySQL table can be updated using PHP.


Related Resources

How to Create a MySQL Table Using PHP

How to Select a Table from a MySQL Database

How to Check if a MySQL Table Exists Using PHP

How to Show All Tables of a MySQL Database Using PHP

How to Delete a MySQL Table Using PHP

How to Modify the Defintion or Data Type of a Column of a MySQL Table Using PHP

How to Insert Data into a MySQL Table Using PHP

How to Delete a Row of a MySQL Table Using PHP

How to Delete All Rows of a MySQL Table Using PHP

How to Insert Data Into a Table of a MySQL Database From an HTML Form Using PHP

How to Copy a Table Definition in MySQL Using PHP

HTML Comment Box is loading comments...