How to update MySQL table in PHP

Hey… how are you people ? Today I am going to explain you about how to update a MySQL table from a PHP page.

Up to now you have learned how to insert data to a MySQL table and how to retrieve data from a MySQL table. Today also I am going to use my usual interface and the only difference is the Update button.

Figure 1

As you can see in the Figure 1, I have retrieved some data from a MySQL table and there the NIC field has disabled for updating. The client can update only the Name and Age fields. I used the disable attribute of the text field as follows.

input name="textfieldNic" type="text" disabled="disabled" id="textfieldNic" value="...php echo $_SESSION['session_nic']; ..."

Don’t forget to include <?php?> tags when you using PHP stuff (Take a good look on value of the value attribute where I haven’t include above mentioned PHP tags because of the formatting get upset in this blog post).

When you click on the Update button, the page will direct to another PHP page which contains following codes.


$var_nic = $_SESSION['session_nic'];
$var_name = $_POST[@textfieldName];
$var_age = $_POST[@textfieldAge];

//Data update in the db

$updateQuery = "UPDATE member_details SET name='$var_name', age='$var_age' WHERE nic='$var_nic'";
$updateResult = mysql_query($updateQuery)or die('Query failed: ' . mysql_error());

//Updated statement
//Redirect statement

So that’s it about the update. Thanks for reading my blog posts and please send your feedbacks about this blog, how I write, where I have done wrong etc. almost any comment.🙂

About AnujAroshA

An undergraduate in the stream of ICT (Information & Communication Technology). A simple person :)
This entry was posted in PHP examples. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s