From 995630915e5cdd728868db3ec3eebc0af024eb83 Mon Sep 17 00:00:00 2001 From: 65160270 <65160270@go.buu.ac.th> Date: Fri, 28 Mar 2025 20:38:47 +0700 Subject: [PATCH] update-cart --- shop-routes/cart.js | 2 +- shop-routes/product.js | 13 ++++++++++--- views/product.ejs | 4 +++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/shop-routes/cart.js b/shop-routes/cart.js index 1d409da..8fc009d 100644 --- a/shop-routes/cart.js +++ b/shop-routes/cart.js @@ -35,6 +35,7 @@ router.get('/', async (req, res) => { // เพิ่มสินค้าลงตะกร้า router.post('/add', async (req, res) => { const { productId, quantity } = req.body; + console.log("Received:", { productId, quantity }); try { const [[product]] = await pool.query( 'SELECT stock FROM products WHERE id = ?', [productId] @@ -81,7 +82,6 @@ router.post('/add', async (req, res) => { // อัพเดทจำนวนสินค้าในตะกร้า router.post('/update', async (req, res) => { - console.log("Received Data:", req.body); const { cartItemId, quantity } = req.body; try { const [[cartItem]] = await pool.query( diff --git a/shop-routes/product.js b/shop-routes/product.js index b9041d8..f19cdb3 100644 --- a/shop-routes/product.js +++ b/shop-routes/product.js @@ -41,17 +41,24 @@ router.get("/add", (req, res) => { }); // เพิ่มสินค้าใหม่พร้อมรูป -router.post("/add", upload.single("image"), async (req, res) => { // เปลี่ยนจาก "images" เป็น "image" ให้ตรงกับ name ของ input file +router.post("/add", upload.single("image"), async (req, res) => { console.log("req.file:", req.file); // ตรวจสอบว่า multer ได้รับไฟล์หรือไม่ + console.log("req.body:", req.body); // ตรวจสอบค่าที่ส่งมาจากฟอร์ม try { - const { name, price, stock, description } = req.body; // เพิ่ม description + const { name, price, stock, description } = req.body; + + // ถ้าไม่มีไฟล์ที่อัปโหลด + if (!req.file) { + console.error("No file uploaded."); + return res.status(400).send("No file uploaded."); + } const imagePath = "/uploads/" + req.file.filename; await pool.execute( "INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)", - [name, price, stock, description, imagePath] // เพิ่ม description + [name, price, stock, description, imagePath] ); res.redirect("/products"); diff --git a/views/product.ejs b/views/product.ejs index 6390a5a..c9b1754 100644 --- a/views/product.ejs +++ b/views/product.ejs @@ -13,7 +13,8 @@ <th>ชื่อสินค้า</th> <th>ราคา</th> <th>จำนวน</th> - <th>จัดการ</th> <!-- เพิ่มคอลัมน์สำหรับปุ่มแก้ไข --> + <th>คำบรรยาย</th> + <th>จัดการ</th> </tr> <% products.forEach(product => { %> <tr> @@ -21,6 +22,7 @@ <td><%= product.name %></td> <td><%= product.price %> บาท</td> <td><%= product.stock %></td> + <td><%= product.description %></td> </tr> <% }); %> </table> -- GitLab