4

我有这个帖子请求

app.post("/msg", (req, res) => {
  console.log(req.body)
  connection.query('INSERT INTO plans (topic, notes, resources) VALUES 
  (?)', [req.body.topic, req.body.note, req.body.resource],(error, 
  results) => {
     if (error) return res.json({ error: error });

     });
 });

我从中得到了这个错误

"error": {
    "code": "ER_WRONG_VALUE_COUNT_ON_ROW",
    "errno": 1136,
    "sqlState": "21S01",
    "sqlMessage": "Column count doesn't match value count at row 1"
}

这是桌子

CREATE TABLE plans(
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  topic VARCHAR(64) NOT NULL,
  notes VARCHAR(200) NOT NULL,
  resources VARCHAR(200) NOT NULL
 );

请问这个请求有什么问题吗?

4

1 回答 1

9

您必须根据您提供的列值的数量提供问号。

app.post("/msg", (req, res) => {
  console.log(req.body)
  connection.query('INSERT INTO plans (topic, notes, resources) VALUES 
  (?,?,?)', [req.body.topic, req.body.note, req.body.resource],(error, 
  results) => {
     if (error) return res.json({ error: error });

     });
 });

这应该工作

于 2018-03-26T08:34:32.570 回答