I'm working with this NextJS code here to try to submit data to my MySQL database:
import mysql from "mysql2/promise";
export default async function handler(req, res) {
const dbconnection = await mysql.createConnection({
host: "localhost",
database: "onlinestore",
port: 3306,
user: "root",
password: "Jennister123!",
});
try {
var products = req.body;
const values = [
products,
];
const query = `INSERT INTO games (ProductID, ProductName, ProductDescription, ProductImage, ProductPrice, DateWhenAdded) VALUES ("${values}")`
console.log(values);
const[data] = await dbconnection.execute(query, values);
console.log(query);
console.log(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 });
}
}
And I'm using this form to do so (newgame.html):
<!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>
However, I'm getting the "You have an error in your SQL syntax" error, and I can't seem to understand why that is. I tried different ways of writing the code down before and changing the values, including editing the query, but then that would leave me with other different errors I'm unsure of how to fix. How can I fix this?