search

Home  >  Q&A  >  body text

"Insert data using mysql2 in Node.js"

<p>I have this POST request</p> <pre class="brush:php;toolbar:false;">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 }); }); });</pre> <p>But I got this error from it</p> <pre class="brush:php;toolbar:false;">"error": { "code": "ER_WRONG_VALUE_COUNT_ON_ROW", "errno": 1136, "sqlState": "21S01", "sqlMessage": "Column count does not match value count for row 1" }</pre> <p>This is the form</p> <pre class="brush:php;toolbar:false;">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 );</pre> <p>What's wrong with the request? </p>
P粉465287592P粉465287592563 days ago453

reply all(1)I'll reply

  • P粉161939752

    P粉1619397522023-08-25 16:17:51

    You must provide question marks based on the number of column values ​​you provide.

    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

    reply
    0
  • Cancelreply