MySQL Update Data

PHP MySQL Update Data

Sometimes users need to change their information (like updating an email address or changing a password). The UPDATE statement is used to modify existing records in a table.


The UPDATE Statement

The syntax to update data looks like this: UPDATE table_name SET column1=value, column2=value2 WHERE some_column=some_value

Just like the DELETE statement, the WHERE clause specifies which record or records should be updated. If you omit the WHERE clause, all records will be updated!

Update Record Example

<?php
// Database connection code omitted...

// Update the lastname of the user with ID 2 $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if (mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($conn); }

mysqli_close($conn); ?>

After running this code, user number 2 will have their last name officially changed to "Doe" in the database.


Exercise

?

Which keyword is used to specify the new values you want to insert during an UPDATE statement?