6

I have this post request

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 });

     });
 });

and i get this error from it

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

this is the table

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
 );

please whats wrong with the request ?

1 Answer 1

16

You have to provide the question mark according to number of column values you are providing.

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 });

     });
 });

this should work

Sign up to request clarification or add additional context in comments.

4 Comments

Why did you put ?, ?, ? after values and then a comma and then put the req.body after that?
Because we are trying to insert 3 values in table row.
Gotcha my bad. I see you made the comment above too. Thank you for the quick response.
Any suggestion on inserting multiple rows?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.