【发布时间】:2016-06-30 15:05:50
【问题描述】:
我尝试向 db 插入加密值,我可以加密无法在 db 中插入加密值的值。
app.post('/insert', function (req, res) {
// var Fname=req.body.fname;
// var Lname=req.body.pwd;
var data = {
Fname: req.body.fname,
Lname: req.body.Lname
};
function hashP(getit, cb) {
bcrypt.genSalt(15, function (err, salt) {
if (err) {
return console.log(err);
}
cb(salt);
bcrypt.hash(getit, salt, function (err, gotit) {
if (err) throw err;
return this.cb(null, gotit);
})
})
}
hashP(data.Lname, function (err, gotit) {
if (err) throw err;
data.Lname = hash;
})
console.log(data.Lname);
con.query("insert into test set ?", [data], function (err, rows) {
if (err) throw err;
res.send("Value has bee inserted");
})
})
这是我的 html 表单页面:
<body>
<form action="http://localhost:8888/insert" method="POST" >
<label>Name:</label><input type="text" name="fname"></br>
<label>Lname:</label><input type="text" name="Lname"></br>
<button type="submit">Submit</button>
</form>
</body>
【问题讨论】:
-
错误是什么?
-
我没有收到任何错误,来自 html 表单的数据直接插入到数据库中。但它不会转换为加密数据。当我签入终端时,我得到两个 Lname 值,一个是直接数据,另一个是加密数据。
-
这看起来不对:
data.Lname = hash;因为hash没有在任何地方定义。您的意思是改用gotit吗? -
是的,你是对的,但是来自 html 的数据已被加密,但加密的值不会存储在 db 中,而不是存储 html 值,我认为我们必须更改查询。