Hacking with PHP has been updated for PHP 7 - click here! >>

Updating data

To update data currently in your database, you use the UPDATE command. You specify all the fields you want to change, and the values you want to change them to like this:

UPDATE usertable SET FirstName = 'Joe', Age = 30;

That statement will change all records in usertable so that they have the FirstName "Joe" and Age "30". We can filter the rows to be updated by using a WHERE clause, just like in SELECT. So, to change all people with FirstName "Joe" to FirstName "Peter", we would use this statement:

UPDATE usertable SET FirstName = 'Peter' WHERE FirstName = 'Joe';

It's quite easy, isn't it? UPDATE can also use the LIMIT clause, to allow you to only update the first n matching rows, but this is rarely used.


If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Deleting data >>

Previous chapter: Extra SELECT keywords

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.