mysql 如果namedPlaceHolder参数未启用,则绑定参数必须为数组错误

nlejzf6q  于 2023-03-17  发布在  Mysql
关注(0)|答案(1)|浏览(316)

由于一个关键问题,我重新设计了我的API。事实上,我有一个“文件”,它是一个多部分/表单数据,我需要一个表单处理程序,因为NextJS没有。所以我围绕“多方”工作,在那里我可以解析来自表单的多部分/表单数据。这是我现在使用的API:

import mysql from "mysql2/promise";
import multiparty from "multiparty";

export default async function handler(req, res) {
  const dbconnection = await mysql.createConnection({
      host: "localhost",
      database: "onlinestore",
      port: 3306,
      user: "root",
      password: "Jennister123!",
  });
  try {
    const form = new multiparty.Form()

    const data = await new Promise((resolve, reject) => {
      form.parse(req, function (err, fields, files) {
        if (err) reject({ err })
        resolve({ fields, files })
      })
    })
const query = `INSERT INTO games (ProductID, ProductName, ProductDescription, ProductImage, ProductPrice, DateWhenAdded) VALUES ("${data.fields.ProductID}", "${data.fields.ProductName}","${data.fields.ProductDescription}","${data.fields.ProductImage}","${data.fields.ProductPrice}","${data.fields.DateWhenAdded}")`
    await dbconnection.execute(query, data);
   
    dbconnection.end();
    
    
    data.forEach((games) => {
        games.ProductImage = "data:image/webp;base64," + games.ProductImage.toString('base64');
    }
    );
    data.forEach((games) => {
        var d = new Date(games.DateWhenAdded);
        var now = moment(d).format('l');
        console.log(now);
    }
    );
    res.status(200).json({ games: data });
    

} catch (error) {
    res.status(500).json({ error: error.message });
}
}
export const config = {
  api: {
    bodyParser: false,
  }
}

显然从另一个问题来看,这是我使用的形式:

<!DOCTYPE html>
<html lang = "en">
<head>
<title>
New Game Addition
</title>
</head>
<body>
<form method="post" action="/api/newgame" enctype="multipart/form-data">
    <input type="number" id = "ProductID" name="ProductID" placeholder="Product ID"> <br />
    <input type="text" id = "ProductName" name="ProductName" placeholder="Name of Product."> <br />
    <input type="text" id = "ProductDescription" name="ProductDescription" placeholder="Describe the Product."> <br />
    <input type="file" id = "ProductImage" name="ProductImage" placeholder="Put in Image"> <br />
    <input type="number" step="0.01" id = "ProductPrice" name="ProductPrice" placeholder="0.00"> <br />
    <input type="date" id = "DateWhenAdded" name="DateWhenAdded" placeholder="01/01/2001"> <br />
    <input type="submit" name="submit" value="SUBMIT">
</form>
</body>
</html>

然而,我有一个问题,我在哪里得到这个错误在我的浏览器上。

{"error":"Bind parameters must be array if namedPlaceholders parameter is not enabled"}

有人知道如何修复此错误,以便我可以运行查询吗?

kt06eoxx

kt06eoxx1#

用这个代码解决了。

import mysql from "mysql2/promise";
import multiparty from "multiparty";

export default async function handler(req, res) {
  const dbconnection = await mysql.createConnection({
      host: "localhost",
      database: "onlinestore",
      port: 3306,
      user: "root",
      password: "Jennister123!",
  });
  try {
    const form = new multiparty.Form()

    const data = await new Promise((resolve, reject) => {
      form.parse(req, function (err, fields, files) {
        if (err) reject({ err })
        resolve({ fields, files })
      })
    })
    const query = "INSERT INTO games (ProductID, ProductName, ProductDescription, ProductImage, ProductPrice, DateWhenAdded) VALUES(?,?,?,?,?,?)";
    const values=[parseInt(data.fields.ProductID), data.fields.ProductName[0], data.fields.ProductDescription[0], data.files.ProductImage[0].toString('base64'), parseFloat(data.fields.ProductPrice), data.fields.DateWhenAdded[0]]

    await dbconnection.execute(query, values);
   
    dbconnection.end();

    res.status(200).json({ games: data });
    

} catch (error) {
    res.status(500).json({ error: error.message });
}
}
export const config = {
  api: {
    bodyParser: false,
  }
}

相关问题