【问题标题】:Node.js mySQL syntax error when posting data发布数据时Node.js mySQL语法错误
【发布时间】:2018-03-31 18:11:15
【问题描述】:

我是 Node.js 和一般服务器的新手,刚刚开始尝试在我的服务器中实现一个数据库。我已经设置了 mySQL 数据库和表,服务器连接到数据库并且服务器开始处理 SQL 查询,但是由于我的 INSERT 中的语法错误,它中断了查询,我一直在查看论坛并有更改了我的代码几次,但没有运气...如果您能看到我哪里出错了,请帮忙,谢谢!

router.post('/', (req, res, next) => {
    console.log(req.name);
    const imageJSON = {
        //attributes of item that server will request (more important for db
        name: req.body.name,
        image: req.body.image
    };
    //console.log(req.body.image);

    res.status(201).json({
        message: imageJSON
    });
    connection.connect(function(err){
        if(!err) {
            console.log("Database is connected ... nn");
        } else {
            console.log("Error connecting database ... nn");
        }
    });
    var post = {id: 1, name: req.body.name, image: req.body.image};
    connection.query('INSERT INTO "images" ("id", "name", "image") VALUES ?', post,
        function(err, rows, fields) {
            connection.end();
            if (!err)
                console.log('The solution is: ', rows);
            else
                console.log('Error while performing Query.', err);
        });
    //connection.end();
});

【问题讨论】:

  • 你不应该在 sql 对象名(表和列名)周围使用引号

标签: mysql node.js node-mysql


【解决方案1】:

试试这个:

var post = {id: 1, name: req.body.name, image: req.body.image};
var query = connection.query('INSERT INTO images SET ?', post, function 
   (error, rows, fields) {
      // ... operation on result
});

【讨论】:

  • 打败我。 +1
猜你喜欢
  • 1970-01-01
  • 2022-01-13
  • 2023-04-02
  • 1970-01-01
  • 2013-01-18
  • 2016-07-23
  • 2021-12-16
  • 1970-01-01
  • 2013-01-13
相关资源
最近更新 更多