0

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"
}
robokonk
  • 101
  • 1
  • 5
  • 1
    Possible causes: date is a keyword, try renaming that column; make sure the date value format is suitable. Update your question with the table columns definition . – not2savvy Nov 22 '22 at 21:14
  • The problem is not that `date` is a keyword. It's fine to use that as a column name because it's not a _reserved_ keyword. Read https://dev.mysql.com/doc/refman/8.0/en/keywords.html – Bill Karwin Nov 22 '22 at 21:23
  • The problem is that you put single-quotes around a column name. Single quotes are for string literals, not column identifiers. Read the linked question. – Bill Karwin Nov 22 '22 at 21:24

0 Answers0