I have problem with syntax mysql in node and mysql lib. I check similar problem on stackoverflow, but not working, I also checking syntax added semicolon instead of comma but still not working.
My code:
export const addPost = (req, res) => {
const token = req.cookies.access_token;
if (!token) return res.status(401).json("Unauthorized");
jwt.verify(token, "jwtkey", (err, userInfo) => {
if (err) return res.status(403).send("Unauthorized");
const q =
"INSERT INTO posts(`title`, `desc`, `img`, `cat`, 'date', `uid`) VALUES (?)";
const values = [
req.body.title,
req.body.desc,
req.body.img,
req.body.cat,
req.body.date,
userInfo.id,
];
db.query(q, [values], (err, data) => {
if (err) return res.status(500).json(err);
return res.status(200).json("Post has been created.");
});
});
};
Error:
code: 'ER_PARSE_ERROR',
errno: 1064,
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 ''date', `uid`) VALUES 'dasd', '<p>dasdasd</p>', 'file-1669150985305-544759965IMG' at line 1",
sqlState: '42000',
index: 0,
sql: "INSERT INTO `posts` (`title`, `desc`, `img`, `cat`, 'date', `uid`) VALUES 'dasd', '<p>dasdasd</p>', 'file-1669150985305-544759965IMG_0366 3.HEIC', 'science', '2022-11-22', 16"
}