From 4e80835d92e3cfdabfc0e22bd3c78f44e1f28ffa Mon Sep 17 00:00:00 2001 From: 65160270 <65160270@go.buu.ac.th> Date: Fri, 28 Mar 2025 21:38:35 +0700 Subject: [PATCH] update-cart --- config/database.js | 4 ++++ shop-routes/product.js | 30 +++++++++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/config/database.js b/config/database.js index 30e3a74..bb4c10f 100644 --- a/config/database.js +++ b/config/database.js @@ -22,4 +22,8 @@ const pool = mysql.createPool({ } })(); +pool.getConnection() + .then(() => console.log("Database connected!")) + .catch((err) => console.error("Database connection error:", err)); + module.exports = pool; \ No newline at end of file diff --git a/shop-routes/product.js b/shop-routes/product.js index b1ddf20..bb5acd6 100644 --- a/shop-routes/product.js +++ b/shop-routes/product.js @@ -42,25 +42,29 @@ router.get("/add", (req, res) => { // เน€เธเธดเนเธกเธชเธดเธเธเนเธฒเนเธซเธกเนเธเธฃเนเธญเธกเธฃเธนเธ router.post("/add", upload.single("image"), async (req, res) => { - console.log("Received Data:", req.body); // เน€เธเนเธเธเนเธฒเธ—เธตเนเธชเนเธเธกเธฒ - console.log("Uploaded file:", req.file); // เน€เธเนเธเธงเนเธฒ multer เนเธ”เนเนเธเธฅเนเนเธซเธก + console.log("req.file:", req.file); // เธ”เธนเธงเนเธฒ multer เนเธ”เนเธฃเธฑเธเนเธเธฅเนเนเธซเธก + console.log("req.body:", req.body); // เธ”เธนเธเนเธฒเธ—เธตเนเธชเนเธเธกเธฒเธเธฒเธเธเธญเธฃเนเธก try { const { name, price, stock, description } = req.body; - const priceNum = parseFloat(price); - const stockNum = parseInt(stock, 10); - if (isNaN(priceNum) || isNaN(stockNum)) { - return res.status(400).send("Price and stock must be valid numbers."); + if (!req.file) { + console.error("Error: No file uploaded!"); + return res.status(400).send("File upload failed"); } - // เธ–เนเธฒเนเธกเนเธกเธตเธเธฒเธฃเธญเธฑเธเนเธซเธฅเธ”เนเธเธฅเน เนเธซเน imagePath เน€เธเนเธ null - const imagePath = req.file ? "/uploads/" + req.file.filename : null; - - await pool.execute( - "INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)", - [name, priceNum, stockNum, description, imagePath] - ); + const imagePath = "/uploads/" + req.file.filename; + console.log("imagePath:", imagePath); // เธ•เธฃเธงเธเธชเธญเธเธเธฒเธเนเธเธฅเนเธฃเธนเธ + + try { + await pool.execute( + "INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)", + [name, price, stock, description, imagePath] + ); + } catch (error) { + console.error("Database Error:", error); + return res.status(500).json({ error: "Database Error", details: error }); + } res.redirect("/products"); } catch (error) { -- GitLab