I’m not able to insert a JSON string into a mySQL table.
CREATE TABLE registered (
id int NOT NULL AUTO_INCREMENT,
name nvarchar(128) NOT NULL,
panels JSON NOT NULL,
PRIMARY KEY (id)
);
app.post("/registered", (req, res) => {
let data = req.body;
let sqlPost = `INSERT INTO registered (name, panels) VALUES ("${data.name}", "${data.panels}")`;
db.query(sqlPost, (error, result) => {
if (error) return res.json(error);
res.send({
message: "Registered.",
});
});
});
${data.panels}
looks like {"panels":["uti","wound"]}
. I verified the JSON format.
The error I’m getting is the following.
code: "ER_PARSE_ERROR"
errno: 1064
sql: "INSERT INTO registered n (name, panels) n VALUES ("safas", "{"panels":["uti","wound"]}")"
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'panels":["uti","wound"]}",' at line 3"
sqlState: "42000"
I’ve been messing around with this too long. What the hell is wrong here?
I tried getting rid of the double quotes around the ${data.panels}. I verified the json.
New contributor
Jeri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.