Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit af93c7c0 authored by 65160270's avatar 65160270
Browse files

update-cart

parent 4ba62519
No related branches found
No related tags found
No related merge requests found
...@@ -41,21 +41,20 @@ router.get("/add", (req, res) => { ...@@ -41,21 +41,20 @@ 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.file:", req.file); // ตรวจสอบว่า multer ได้รับไฟล์หรือไม่
try { try {
const { name, price, stock, description } = req.body; // เพิ่ม description const { name, price, stock } = req.body;
if (!name || !price || !stock || !description || !req.file) { if (!name || !price || !stock || !req.file) {
return res.status(400).send("กรุณากรอกข้อมูลให้ครบถ้วน และเลือกรูปภาพ"); return res.status(400).send("กรุณากรอกข้อมูลให้ครบถ้วน และเลือกรูปภาพ");
} }
const imagePath = "/uploads/" + req.file.filename; const imagePath = "/uploads/" + req.file.filename;
await pool.execute( // แก้จาก 'image' เป็น 'image_url'
"INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)", await pool.execute("INSERT INTO products (name, price, stock, image_url) VALUES (?, ?, ?, ?)",
[name, price, stock, description, imagePath] // เพิ่ม description [name, price, stock, imagePath]);
);
res.redirect("/products"); res.redirect("/products");
} catch (error) { } catch (error) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment