nodejs mysql插入问题 [英] nodejs mysql insert issue
本文介绍了nodejs mysql插入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
exports.adduser = function(connection) {
return function(req, res) {
// get form values
var username = req.body.username;
var firstname = req.body.firstname;
var lastname = req.body.lastname;
var post = {
username : username,
firstname : firstname,
lastname : lastname
};
connection.query('INSERT INTO myDatabase VALUES ?', post, function(err, result) {
if (err) {
res.send("There was a problem adding the information to the database.");
}
});
}
}
这似乎不起作用.有人看到任何明显的问题吗?语法正确吗?当我按下表单上的提交"按钮时,它只是挂起,并且插入从未发生.我已经确认表格获得正确的值.数据库表中的字段是用户名,名字和姓氏.
This doesn't seem to work. Does anyone see any glaring issues? Is the syntax correct? When I press the submit button on my form, it just hangs, and the insert never occurs. I have confirmed that the form gets the right values. The fields in the database table are username, firstname, and lastname.
推荐答案
您误读了本手册!如果您正在使用INSERT INTO
的对象,则需要使用SET
:
You misread the manual! If you are using an object to INSERT INTO
you need to use SET
:
INSERT INTO myDatabase SET ?
如果您注意了,您可能已经注意到,这种转义可以使您执行以下整洁的事情:
If you paid attention, you may have noticed that this escaping allows you to do neat things like this:
var post = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
// Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
这篇关于nodejs mysql插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文