【问题标题】:Express/Node user controller to upload file on server and MySQL throw an SQL syntax errorExpress/Node 用户控制器在服务器上上传文件和 MySQL 引发 SQL 语法错误
【发布时间】:2022-01-15 18:15:00
【问题描述】:

我正在按照以下代码创建用户控制器 export.update

在前端提交POST 请求后,控制器应执行以下操作:

  • 上传文件夹updload上的文件;
  • 在 MySql 数据库中插入图片的名称。

当我点击提交按钮时,文件在专用文件夹上的上传成功,但没有数据传输到 MySql。相反,我在终端上收到了这条消息:

sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE id = '203'' at line 1", sqlState: '42000', index: 0, sql: "UPDATE user SET first_name='John' ,last_name='Doe', profile_image='rocket-lab-New-Zealand.jpeg',WHERE id = '203'"

非常感谢任何关于正确道路的建议/方向。

exports.update = (req, res) => {
    message = '';
    if (req.method == 'POST') {
        var post = req.body;
        var first_name = post.first_name;
        var last_name = post.last_name;


        if (!req.files)
            return res.status(400).send('No files were uploaded.');

        var file = req.files.profile_image;


        var profile_image = file.name;
        console.log(profile_image)
        if (file.mimetype == "image/jpeg" || file.mimetype == "image/png" || file.mimetype == "image/gif") {

            file.mv('./upload/' + file.name, function (err) {

                if (err)

                    return res.status(500).send(err);

                connection.query('UPDATE user SET first_name=? ,last_name=?, profile_image=?,WHERE id = ?', [first_name, last_name, profile_image, req.params.id], (err, rows) => {

                    if (!err) {
                        connection.query('SELECT * FROM user WHERE id = ?', [req.params.id], (err, rows) => {
                            if (!err) {
                                res.render('edit-crew', { rows, alert: `${first_name} has been updated.` });

                            } else {
                                console.log(err);
                            }
                            console.log('The data from user table:\n', rows);
                        });
                    } else {
                        console.log(err);
                    }
                    console.log('The data from user table:\n', rows);
                });
            });
        }
    }
}

【问题讨论】:

    标签: mysql node.js express


    【解决方案1】:
    UPDATE user SET first_name=? ,last_name=?, profile_image=?,WHERE id = ?
    

    在 WHERE 子句之前有一个额外的 ","。

    修复:

    UPDATE user SET first_name=? ,last_name=?, profile_image=? WHERE id = ?
    

    【讨论】:

    • 是的!你是对的,谢谢现在正在工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多