Update Data In a Database

The UPDATE statement is used to update existing records in a table.
Syntax:
UPDATE table_name
SET column1=value, column2=value2,…
WHERE some_column=some_value

Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated! Earlier in the tutorial we created a table named “registration_form“. Here is how it looks:

Firstname Lastname Age Emailadd Address
Ivan Smith 15 ivansmith@yahoo.com Los Angeles
Draether Uy 17 draether17@yahoo.com California
Glenn Lee 19 lee19143@yahoo.com South Korea

The following example updates some data in the “registration_form” table:
I used NetBeans IDE 6.9.1 (The NetBeans IDE is an award-winning integrated development environment available for Windows, Mac, Linux, and Solaris. The NetBeans project consists of an open-source IDE and an application platform that enable developers to rapidly create web, enterprise, desktop, and mobile applications using the Java platform, as well as JavaFX, PHP, JavaScript and Ajax, Ruby and Ruby on Rails, Groovy and Grails, and C/C++. ) For more details, CLICK HERE.

See the code below:

<?php
$con= mysql_connect(“localhost”,”root”,””);
if(!con)
{
die(‘could not connect:’.mysql_error());
}
mysql_select_db(“engr_department”,$con);

mysql_query(“UPDATE registration_form SET age=’20’ WHERE firstname=’Ivan’ AND lastname=’Smith'”);

mysql_close($con);
?>

In NetBeans IDE 6.9.1, this is how the code goes:
Where we updated the age of Ivan Smith to 20.

After the update, the “registration_form” table will look like this:

Firstname Lastname Age Emailadd Address
Ivan Smith 20 ivansmith@yahoo.com Los Angeles
Draether Uy 17 draether17@yahoo.com California
Glenn Lee 19 lee19143@yahoo.com South Korea