首页 > 解决方案 > 如何修复我的 express POST 处理程序中的 SQL 语法错误?

问题描述

第一次在这里发帖。

每当我使用代码中的处理程序发送发布请求时,我都会不断收到以下错误。

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check that manual that corresponds to your MariaDB server version for the right syntax to use near '1)' at line 1.
app.post('/',function(req,res,next){
var context = {};
pool.query("INSERT INTO workouts (`name`, `reps`, `weight`,`date`, 'lbs') VALUES (?, ?, ?, ?, ?)", [req.body.name, req.body.reps, req.body.weight, req.body.date, req.body.lbs], function(err, result){
    if(err){
        next(err);
        return;
    }
    context.results = "Inserted id " + result.insertId;
    res.render('table', context);
});

我正在为一个课程项目做这个,我无法弄清楚问题是什么。谢谢您的帮助!

更新:显然,我需要将日期更改为“YYYY-MM-DD”格式。谢谢您的帮助!

标签: mysqlsqlnode.js

解决方案


显然它是 MariaDB 或 MySQL,看起来你在 lbs 周围有单引号。从单引号'

INSERT INTO workouts (`name`, `reps`, `weight`,`date`, **'lbs'**)

支持引号`

INSERT INTO workouts (`name`, `reps`, `weight`,`date`, `lbs`)

推荐阅读