diff --git a/shop-routes/cart.js b/shop-routes/cart.js index 1d409da04476b72d8d21f1eb8aa608f1d1e28d9a..8fc009d586f7a554757a536b25ed8ae73973daef 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 b9041d845529484d5c7a6134f04a7f1bd39d1dc9..f19cdb3ac33c017c9101ded2f04c89e8ea2c6f9c 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 6390a5a497ac1c7b5667afc96f2a5f2f4ac8dffa..c9b175463fca8080a09b45e245cfe6b2e7d5a5b3 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>