'SqlError: (conn=27775, no: 1064, SQLState: 42000)

I keep getting an error when trying to update a table on my MariaDB database. I have a products table and need to update one of the rows. I have tried changing the update statement with single quotes but it still results in an error. MariaDB guide shows similar update syntax so I'm not sure why I keep getting an error.

Here is the error I get:

SqlError: (conn=27775, no: 1064, SQLState: 42000) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
sql:  UPDATE product SET productname = ?, prodimage = ?, description = ?, category_id = ?, supplier_id = ?, dimensions = ?, wattage = ?, cell_efficiency = ?, weight = ?, power_tolerance = ?,  prodprice = ?, status = ?, quantity = ? WHERE product_id =  - paramet...]
    at Object.module.exports.createError 
(C:\node_modules\mariadb\lib\misc\errors.js:56:10)
    at PacketNodeEncoded.readError 
(C:\node_modules\mariadb\lib\io\packet.js:537:19)
    at Query.readResponsePacket 
(C:\node_modules\mariadb\lib\cmd\parser.js:52:28)
    at PacketInputStream.receivePacketBasic 
(C:\node_modules\mariadb\lib\io\packet-input-stream.js:76:9)
    at PacketInputStream.onData 
(C:\node_modules\mariadb\lib\io\packet-input-stream.js:139:20)
    at Socket.emit (node:events:520:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Socket.Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  text: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1",
  sql: ' UPDATE product SET productname = ?, prodimage = ?, description = ?, category_id = ?, supplier_id = ?, dimensions = ?, wattage = ?, cell_efficiency = ?, weight = ?, power_tolerance = ?,  prodprice = ?, status = ?, quantity = ? WHERE product_id =  - paramet...]',
  fatal: false,
  errno: 1064,
  sqlState: '42000',
  code: 'ER_PARSE_ERROR'
}

Here is the table schema.

CREATE TABLE product (
    product_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    productname VARCHAR(100) NOT NULL,
    prodimage VARCHAR(50) NOT NULL,
    description VARCHAR(500) NOT NULL,
    category_id INT NOT NULL,
    supplier_id INT NOT NULL,
    dimensions VARCHAR(100) NOT NULL,
    wattage INT NOT NULL,
    cell_efficiency DECIMAL NOT NULL,
    weight DECIMAL NOT NULL,
    power_tolerance INT NOT NULL,
    prodprice DECIMAL(8,2) NOT NULL,
    status VARCHAR(20) NOT NULL,
    quantity INT NOT NULL,
    FOREIGN KEY (category_id) REFERENCES category(category_id) ON DELETE CASCADE ON UPDATE RESTRICT,
    FOREIGN KEY (supplier_id) REFERENCES supplier(supplier_id) ON DELETE CASCADE ON UPDATE RESTRICT
);

This is my code for updating the table row

router.post("/save", function (req, res, next) {
  let updatequery =
    "UPDATE product SET productname = ?, prodimage = ?, description = ?, category_id = ?, supplier_id = ?, dimensions = ?, wattage = ?, cell_efficiency = ?, weight = ?, power_tolerance = ?,  prodprice = ?, status = ?, quantity = ? WHERE product_id = " + req.body.product_id;
  db.query(
    updatequery,
    [
      req.body.productname,
      req.body.prodimage,
      req.body.description,
      req.body.category_id,
      req.body.supplier_id,
      req.body.dimensions,
      req.body.wattage,
      req.body.cell_efficiency,
      req.body.weight,
      req.body.power_tolerance,
      req.body.prodprice,
      req.body.status,
      req.body.quantity,
    ],
    (err, result) => {
      if (err) {
        console.log(err);
        res.render("error", {message:err.message ,error: err });
      } else {
        res.redirect("/product");
      }
    }
  );
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source