Gitlab@Informatics

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

update-cart

parent d8295efd
No related branches found
No related tags found
No related merge requests found
...@@ -22,4 +22,8 @@ const pool = mysql.createPool({ ...@@ -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; module.exports = pool;
\ No newline at end of file
...@@ -42,25 +42,29 @@ router.get("/add", (req, res) => { ...@@ -42,25 +42,29 @@ router.get("/add", (req, res) => {
// เพิ่มสินค้าใหม่พร้อมรูป // เพิ่มสินค้าใหม่พร้อมรูป
router.post("/add", upload.single("image"), async (req, res) => { router.post("/add", upload.single("image"), async (req, res) => {
console.log("Received Data:", req.body); // เช็คค่าที่ส่งมา console.log("req.file:", req.file); // ดูว่า multer ได้รับไฟล์ไหม
console.log("Uploaded file:", req.file); // เช็คว่า multer ได้ไฟล์ไห console.log("req.body:", req.body); // ดูค่าที่ส่งมาจากฟอร์
try { try {
const { name, price, stock, description } = req.body; const { name, price, stock, description } = req.body;
const priceNum = parseFloat(price);
const stockNum = parseInt(stock, 10);
if (isNaN(priceNum) || isNaN(stockNum)) { if (!req.file) {
return res.status(400).send("Price and stock must be valid numbers."); console.error("Error: No file uploaded!");
return res.status(400).send("File upload failed");
} }
// ถ้าไม่มีการอัปโหลดไฟล์ ให้ imagePath เป็น null const imagePath = "/uploads/" + req.file.filename;
const imagePath = req.file ? "/uploads/" + req.file.filename : null; console.log("imagePath:", imagePath); // ตรวจสอบพาธไฟล์รูป
try {
await pool.execute( await pool.execute(
"INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)", "INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)",
[name, priceNum, stockNum, description, imagePath] [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"); res.redirect("/products");
} catch (error) { } catch (error) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment