我正在尝试使用node.js插入一些数据.我写了以下代码,并通过npm安装了MysqL支持,但是我未能插入表中.
这是我的代码:
var MysqL = require('MysqL'); function BD() { var connection = MysqL.createConnection({ user: 'root',password: '',host: 'localhost',port: 3306,database: 'nodejs' }); return connection; } app.post("/user/create",function(req,res) { var objBD = BD(); var post = { username: req.body.username,password: req.body.password }; objBD.query('INSERT INTO users VALUES ?',post,function(error) { if (error) { console.log(error.message); } else { console.log('success'); } }); });
<form action="/user/create" method="POST" class="form-horizontal"> <input type="text" id="username_input" name="username"> <input type="text" id="password_input" name="password"> <input type="submit" name="Submit" value="Insert" class="btn"> </form>
错误信息是:
You have an error in your sql Syntax; check the manual that corresponds to your MysqL server version for the right Syntax to use near .’username’ = ‘Test’,‘password’ = ‘test’ at line 1
我的错误是什么?
解决方法
注意
documentation of
node-mysql
:
If you paid attention,you may have noticed that this escaping allows you to do neat things like this:
06000
请注意,它们使用SET而不是VALUES. INSERT INTO … SET x = y是有效的MySQL查询,而INSERT INTO … VALUES x = y不是.