"Insert data using mysql2 in Node.js"
P粉465287592
2023-08-24 17:15:24
<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>
You must provide question marks based on the number of column values you provide.
This should work