【发布时间】:2014-01-19 05:30:51
【问题描述】:
请我对 node js 和 expressjs 框架非常陌生。我正在开发一个 Web 应用程序,但我被卡住了。我已经设置了所有数据库要求并且一切正常,现在我想将一些数据保存到数据库中,并且我将数据存储在一个变量中,但不幸的是存储在变量(MyID)中的数据没有保存。这是我的代码
var=我的ID
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");
});
});
}
【问题讨论】:
-
你有一个 SQL 注入漏洞。
-
您的第二个查询在第一个查询完成之前运行
-
退后一步,确保您的数据库连接本身正常工作,然后再进行动态查询。然后记录来自您的连接的响应。是不是出错了?它说什么?
-
我的数据库连接工作正常,当我用原始数据替换变量 MyID 时,例如:2 它会保存,但是当我将 2 存储在 MyID 中时它不会保存。我认为 SLaks 是对的,但我真的不知道如何解决这个问题。
标签: javascript node.js express