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.
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.
... session_start(); include("functions.php"); $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.🙂