Node JS Tutorial | Update MySQL Table Data

In Web Development


Node JS Tutorial | Update MySQL Table Data - read the full article about Node Js update, Web Development and from Webslesson on Qualified.One
alt
Webslesson
Youtube Blogger
alt

hello friends in this video tutorial we will show you how to update or edit mysql table data under node.js express application in previous part we have already show you how to insert form data into mysql database in node.js application so now we have moved to update mysql table data using nodejs so first we need to create edit button in each row of data so we have go to text editor and go into views directory and here we have to open sampledata.a.js template file and here we can see that display mysql table data in html table format code now here we need to create edit button so here we have to create anchor tag [Music] and here in hraf attribute we have to write [Music] slash sample data slash edit and for print dynamic 8 table column data here we have to write open and close node code syntax [Music] and for print value here we have to write equal to sign and after this we have to write data dot it table column so it will print dynamic 8 table column data in each row of data after this in class attribute we have to write button button primary and button small class and in anchor text we have to write edit so it will create edit button now we need to check edit button is created or not so here in command prompt first we have go to our node application directory and after this we have to run following command which will start node.js server now in the browser we have to write localhost with 3000 port number sample data [Music] so it has display mysql table data on web page in html table format and here in each of data we can see that edit button has been created so when we have click on this button then one form must be load [Music] with fill data so we can edit or change so for load form with data we have go to roots folder and here we have to open sampledata.js file and under this file we have to create one root for load edit form in the browser so for this here we have to write router variable dot get function with two parameter so here in the first parameter we have to write slash edit slash double colonnade so here it value will be dynamic [Music] and in the second parameter we have to write callback function with three parameter like request response and next variable under this function first we want to get value of it parameter value so for this here we have to write if variable is equal to request dot params dot it so here we have store it parameter value in the local variable next we need to write query for fake single row of data so for this here we have to write query variable is equal to [Music] select star from sample data table where it is equal to dollar sign with open and close bracket and between this we have to write it variable so this query will page single row of data now we need to execute this query so for this here we have to write database dot query function so this function will execute above mysql query so in this function first parameter we have to write query variable and in second parameter we have to write callback functions with error and data variable [Music] under this function we have to write response dot render function [Music] with two parameter so in the first parameter we have to write view file name sample data and in the second parameter we need to define data which we want sent to views file so here we have to write title key with value like edit mysql table data after this we have to write action key with value like edit and for send mysql data here we have to write sample data with data 0 index now we have go to sample data template file and under this file first we have to define condition for load edit form data [Music] so here we have to write open and close node port syntax and between this we have to write else if statement and under condition [Music] we have to write action variable value as equal to edit so if this condition true then it will execute if block of code and under this block we want to load edit forms with data under this block first we have to paste this bootstrap 5 card code after this we have go to add block of code and here we have copy this form code [Music] and after this here in the edit block of code we have paste it so in the form first we have go to this form action attribute value and here we have to write slash sample data slash edit and after this we have to write open and close node port syntax and after this for print data here we have to write is equal to sign and after this we have to write sample data dotted [Music] after this fulfill first name field with data so here we have to write open and close note code syntax with is equal to sign and after this we have to write sample data dot first name table column [Music] after this for fill last name text box with data so here we have to write open and close note code syntax with is equal to sign and after this we have to write sample data dot last name table column same way we want to fill this h text box with data so here we have to write open and close note code syntax with is equal to sign and after this we have to write sample data dot h table column next we want to set value of gender select box so we have go to javascript code and here we have to write document.getelementbyid method [Music] and under this we have to write it of gender select box after this we have to write value as equal to open and close node port syntax and after this we have to write is equal to sign with sample data dot gender table column name so here our edit form is ready with fill single row of data [Music] now we have go to browser and here we have to click on the edit button [Music] so after click on the edit button here we can see that edit form has been loaded in the browser with fill form data now we have to proceed for submit form data so we have go to sample data roots file and here we have to write router variable dot post function with two parameter so here in the first parameter we have to write [Music] slash edit slash double colon it and in the second parameter we have to write callback function with three parameter like request response and next variable under this function first need to store form data in the local variable so for this first we need to store it parameter value in the local variable so for this here we have to write it variable is equal to request dot params.

after this here we have to write first name variable is equal to request dot body dot first name so here we have store form first name field data into local variable after this for store last name field data here we have to write last name variable is equal to request dot body dot last name [Music] next for storage form data into local variable so for this here we have to write age variable is equal to request.body.h and lastly for store gender form field data into local variable so for this here we have to write gender variable is equal to [Music] request.body.gender so this way we have stored form data into local variable now we have to write update data mysql query so for this here we have to write query variable is equal to update sample data table set first name is equal to dollar signed with open and close bracket with first name variable last name is equal to dollar sign open and close bracket with last name variable age is equal to dollar sign open and close bracket with age variable gender is equal to dollar sign with open and close bracket with gender variable [Music] and in where condition we have to write it is equal to dollar signed with open and close bracket with it so this update query will update mysql table data now we need to execute above query so for this here we have to write database dot query function with two parameter so this function will execute above query in first parameter we have to write query variable and in second argument we have to write callback function with error and data variable under this function we have to write if statement and under condition we have to write error variable so this condition will check if there is any mysql error occur then it will execute if block of code and under this block we have to write throw error variable which will display error on the web page but suppose there is no any error occur and data successfully updated then it will execute else block of code and under this block we have to write response variable dot redirect function and under this function we have to write sample data so it will redirect web page to another page on which it will display mysql table data on web page in html table format so here our code is ready now we have to check output in the browser friends first we have to refresh web page and after refresh of web page here we can see that edit form with fill data has been displayed on the web page now we have change age details and after change age details we have to click on the edit button and after click on the edit button here we can see that web page has been redirected and here we can see list of mysql table data and here we can see that mysql table data has been updated so this way we can edit mysql table data in node express application and in this part we have first page single row of data and load that data into html form and after this we have to write mysql update data script using nodejs so in this part we have learnt how to update mysql table data in node.js in next part we will learn how to delete data from mysql table using node.js so we will be meet in the next part lastly thank you for watching this tutorial and do not forget to subscribe our channel

Webslesson: Node JS Tutorial | Update MySQL Table Data - Web Development