diff --git a/shop-routes/product.js b/shop-routes/product.js index f19cdb3ac33c017c9101ded2f04c89e8ea2c6f9c..b9041d845529484d5c7a6134f04a7f1bd39d1dc9 100644 --- a/shop-routes/product.js +++ b/shop-routes/product.js @@ -41,24 +41,17 @@ router.get("/add", (req, res) => { }); // เพิ่มสินค้าใหม่พร้อมรูป -router.post("/add", upload.single("image"), async (req, res) => { +router.post("/add", upload.single("image"), async (req, res) => { // เปลี่ยนจาก "images" เป็น "image" ให้ตรงกับ name ของ input file console.log("req.file:", req.file); // ตรวจสอบว่า multer ได้รับไฟล์หรือไม่ - console.log("req.body:", req.body); // ตรวจสอบค่าที่ส่งมาจากฟอร์ม try { - const { name, price, stock, description } = req.body; - - // ถ้าไม่มีไฟล์ที่อัปโหลด - if (!req.file) { - console.error("No file uploaded."); - return res.status(400).send("No file uploaded."); - } + const { name, price, stock, description } = req.body; // เพิ่ม description const imagePath = "/uploads/" + req.file.filename; await pool.execute( "INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)", - [name, price, stock, description, imagePath] + [name, price, stock, description, imagePath] // เพิ่ม description ); res.redirect("/products"); diff --git a/views/product.ejs b/views/product.ejs index c9b175463fca8080a09b45e245cfe6b2e7d5a5b3..f0d89b1fa9c4e22169fa0e5a56fd8ca6eb1f7563 100644 --- a/views/product.ejs +++ b/views/product.ejs @@ -14,7 +14,6 @@ <th>ราคา</th> <th>จำนวน</th> <th>คำบรรยาย</th> - <th>จัดการ</th> </tr> <% products.forEach(product => { %> <tr>