首次使用MySQL
(在node/react
应用程序中),并且在尝试向表中插入一些值时遇到错误。我收到的特定错误消息是:
错误:ER_BAD_FIELD_ERROR:“字段列表”中的未知列“名称”
这是我的桌子:
const table =
"CREATE TABLE IF NOT EXISTS
posts(id int AUTO_INCREMENT,
name VARCHAR(30),
email VARCHAR(50),
content VARCHAR(500),
stamp VARCHAR(40),
PRIMARY KEY(id))";
这是查询:
// Insert post 1
app.get("/addpost1", (req, res) => {
let post = {
name: "Joe Blogs",
email: "jblogs@gmail.com",
content:
"Interesting post Phil. It's great to see that a blog really can come alive when the comments update in real-time. The commenting system becomes a conversation platform.",
stamp: "July 30 2019"
};
let sql = "INSERT INTO posts SET ?";
let query = db.query(sql, post, (err, result) => {
if (err) throw err;
console.log(result);
res.send("Post 1 added...");
});
});
与此有关,我一直遇到错误。任何帮助表示赞赏。
答案 0 :(得分:1)
这是正确的语法:
INSERT INTO `table`(`column1`, `column2`) VALUES ([value-1],[value-2])
所以在您的代码中就像:
INSERT INTO `posts` (`name`, `email`, `content`) VALUES ([name:], [email:], [content:])