问题描述
下面是我在 ExpressJS 中的 MYSQL 中插入数据的代码.
Below is my code for inserting data in MYSQL in ExpressJS.
router.post('/user', function (req, res) {
let sql = "INSERT INTO Member (id, Memb_ID, First_Name, Middle_Name, Last_Name, Address, url) VALUES (null, '" + req.body.Memb_ID + "', '" + req.body.First_Name + "', '" + req.body.Middle_Name + "', '" + req.body.Last_Name + "', '" + req.body.Address + "', '" + req.body.url + "')";
myDB.query(sql, function (err, results) {
if (err) throw err;
res.send({
Success: "Data inserted."
})
});
});
它在我的数据库中插入数据,但我的数据库看起来像这样
It inserts data in my database but my database looks like this
我不知道为什么会这样输出.这是我的 JSON
I don't know why it outputs like that. Here's my JSON
编辑:这是我的 MYSQL 表
EDIT : Here is my MYSQL Table
推荐答案
你能确定下面的都做完了吗?
Can you make sure that the following is done?
请求中的
content-type
标头必须是application/json
使用 body-parser(或类似库)来解析 JSON 负载 -> 您可以打印 req.body 来验证这一点.
Use body-parser (or similar libraries) to parse JSON payloads -> You can print the req.body to validate this.
如果您可以确认这些值是可访问的,我会建议转义查询值,如 https://github.com/mysqljs/mysql#escaping-query-values(或在您使用的任何 mysql 库中)——尤其是因为它是一个 DML 查询.
If you can confirm that the values are accessible, I would suggest escaping query values as mentioned in https://github.com/mysqljs/mysql#escaping-query-values (or in any mysql library that you use) - especially since it is a DML query.
(我还假设查询中使用的字段名与数据库中的字段名匹配,并且数据类型正确.)
(Also I assume that the fieldnames used in the query match with the ones in the database and are of the right datatype.)
这篇关于为什么即使我在 POSTMAN 中插入数据,我的数据库仍显示未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!