加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

INSERT INTO与node-mysql失败

发布时间:2020-12-12 16:21:15 所属栏目:MsSql教程 来源:网络整理
导读:我正在尝试使用node.js插入一些数据.我写了以下代码,并通过npm安装了MySQL支持,但是我未能插入表中. 这是我的代码: var mysql = require('mysql');function BD() { var connection = mysql.createConnection({ user: 'root',password: '',host: 'localhost',
我正在尝试使用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');    
        }
    });
});

HTML代码:

<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不是.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读