Please i am very new to node js and expressjs framework. I am developing a web Application and i am stuck. I have setup all my database requirements and all is working just fine , now i want to save some data into the database and i have the data store in a variable, but unfortunately the data stored in the variable(MyID) is not saved. This is my code
var= MyID
app.post("/addContact", function(req, res){
var postData = {
// MyID: req.body.txtemplyeeID************Get employee ID as a foreign Key and Insert it,
EmployeeID:req.body.txtemployeeID,
FatherName:req.body.txtfathersName,
MotherName:req.body.txtMothersName,
NameOfSpouse:req.body.txtSpouseName,
SpouseAddress:req.body.txtSpouseAddress,
ParentsAddress:req.body.txtParentsAddress,
EmployeeAddress:req.body.txtEmployeeAddress
};
connection.connect();
connection.query('SELECT MyID from employees where EmployeeID='+"'"+ postData.EmployeeID +"'" +'', function(err, rows, fields) {
if (err) throw err;
for (var i in rows)
{
var results = rows[i];
MyID=results.MyID;
console.log(MyID);
}
})
connection.query('INSERT INTO EmployeeContacts(MyID,FatherName,MotherName,NameOfSpouse,SpouseAddress,ParentsAddress,EmployeeAddress) VALUES ('+"'"+ MyID +"'"+','+"'"+ postData.FatherName +"'" +','+"'"+ postData.MotherName +"'" +','+"'"+ postData.NameOfSpouse +"'" +','+"'"+ postData.SpouseAddress +"'" +','+"'"+ postData.ParentsAddress +"'" +','+"'"+ postData.EmployeeAddress +"'" +');',
function(err,results){
if(err)console.log(err);
else
console.log("Success");
connection.end() ;
res.redirect("/addContact");
});
});
}