Javascript 在Express.js中插入不起作用的查询
我无法将数据插入MySQL数据库。Select查询工作得很好,所以我假设在Express代码或HTML中遗漏了一些愚蠢的东西。我运行查询的页面位于Javascript 在Express.js中插入不起作用的查询,javascript,mysql,node.js,express,Javascript,Mysql,Node.js,Express,我无法将数据插入MySQL数据库。Select查询工作得很好,所以我假设在Express代码或HTML中遗漏了一些愚蠢的东西。我运行查询的页面位于localhost:8080/add,我正在尝试插入到。这是我的密码: Javascript app.get('/add', function(req, res) { res.sendFile(path.join(__dirname + '/Views/add.htm')); }); app.post('/add', function(req
localhost:8080/add
,我正在尝试插入到
。这是我的密码:
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查询从中读取数据,但我无法插入数据库。请查看此处的文档
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
请查看此处的文档
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
Values(?)
需要圆括号,我会认为Values(?,?)
如果插入3列,但如果使用参数替换,则也可能需要准备required@RiggsFolly我刚刚编辑了我的问题以进一步解释我的问题,希望它能有所帮助。VALUES(?)
需要圆括号,我会认为VALUES(?,?)
如果插入3列,但如果使用参数替换,则也可能需要准备required@RiggsFolly我刚刚编辑了我的问题以进一步解释我的问题,希望它能有所帮助。