MySQL UPDATE

From JumbaWiki

Jump to: navigation, search

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.

PHP

//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();
}

ASP


See also

MySQL Basics
Connect/Disconnect | Select | Insert | Update | Delete
Personal tools
Australian Photographs | Unique Views of Reality | Daedalus