Updating Data

Once your tables contain some data, you have the option of changing existing records. The most frequent reason for doing this would be if information were entered incorrectly—or in the case of user information, if data gets changed (such as a last name or email address) and that needs to be reflected in the database.

The syntax for updating records is

UPDATE tablename SET column='value'

You can alter multiple columns of one record at a single time, separating each from the next by a comma.

UPDATE tablename SET column1='value',
→ column2='value2'...

Normally you will want to use a WHERE clause to specify what rows to affect; otherwise, the change would be applied to every row.

UPDATE tablename SET column1='value'
→ WHERE column2 ...

Get PHP and MySQL for Dynamic Web Sites: Visual Quickpro Guide, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.