MySQL UPDATE Statement

Submitted by:David Villa

Date added:14 May, 2011

Category:MySQL

This is a general code to use when updating an item from a MySQL database. This code isn't considered definitive and can be written in many varied ways.

Note: This code requires you to connect to the MySQL database to work. Also, all field and table names need to be set up to match.

Tags: update statement , mysql statement

Code Snippet:

//This example shows how to update a variable in a databse, where the database field 'id' matches.

//Variables:
$id = 1;
$my_variable1 = "update me";
$my_variable2 = "update me too!";

//MySQL string:
$sql = "UPDATE table_name SET
field_name1 = '$my_variable1',
field_name2 = '$my variable2'

WHERE id='$id'";

$result = mysql_query($sql);

//Output a result
if($result){
echo "Update Successful!";
} else {
$message = "Failed to update! This is the MySQL Error output:<br>" + mysql_error();
}
 
 

Comments