我正在尝试向本地MySQL服务器发出POST请求。每当我提出请求时,我的API错误均显示为:
错误:ER_BAD_FIELD_ERROR:“字段列表”中的未知列“ QU204”
请求的数据是由React前端生成的,但是为了测试这一点,我使用了半编码值。
我要插入的数据的形式为:
var data = {
changeId: "QU204", //varchar
date: 1559902443, //int
client: "Quack", //varchar
impact: 1, //int
type: 1, //int
status: 1, //int
Description: "IMNEW",//varchar
Comments: "HINEWIMACOMMENT" //varchar
}
(另外,如果我在字段中放置任何空格,那么也会出现此错误)
我最初遇到的问题是请求未获取任何数据,但这可以通过使用body-parser来解决。
POST请求看起来像这样:
router.post('/change', function(req, res, next) {
const con= mysql.createConnection({
host: "localhost",
user: "root",
password: "******",
database: "changecontrol"
});
var sql=
"INSERT INTO `Change`(changeId,date,client,impact,type,status,Description,Comments) VALUES("
+ req.body.changeId +","
+ req.body.date + ","
+ req.body.client + ","
+ req.body.impact + ","
+ req.body.type + ","
+ req.body.status + ","
+ req.body.Description + ","
+ req.body.Comments +");";
con.query(sql, function (error, results,fields) {
if(error) throw error;
res.send(JSON.stringify(results));
});
});
如果对VALUES进行了硬编码,则不会出现e / g VALUES(“ QU204” + ...
预期的输出是将较早的数据插入MySQL表的一行中。 朝正确方向前进的任何努力将不胜感激。