[英]INSERT INTO query not working in Express.js
我在将数据插入MySQL数据库时遇到问题。 选择查询工作正常,所以我假设我错过了一些愚蠢的东西,无论是在Express代码中,还是在我的HTML中。 我正在运行查询的页面位于localhost:8080/add
,我正在尝试INSERT INTO
。 这是我的代码:
使用Javascript
app.get('/add', function(req, res) {
res.sendFile(path.join(__dirname + '/Views/add.htm'));
});
app.post('/add', function(req, res) {
var fName = req.body.fName;
var email = req.body.email;
var id = req.body.id;
var post = {id: id, user: fName, email: email};
console.log(post);//This holds the correct data
connection.query('INSERT INTO user VALUES ?', post, function(err, result) {
if (!err) {
console.log('Successfully added information.');
} else {
console.log('Was not able to add information to database.');
}
});
});
我的HTML只是一个提交按钮和3个输入字段,在POST
方法表单中。 再次,我可以连接到数据库并使用select查询从中读取,我只是无法插入其中。
请查看https://github.com/mysqljs/mysql#escaping-query-values中的文档。
connection.query('INSERT INTO posts SET ?', post, function(err, result) {
if (!err) {
console.log('Successfully added information.');
} else {
console.log(result);
console.log('Was not able to add information to database.');
}
});
有效的mysql语句是SET
而不是Values
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.