diff --git a/app.js b/app.js index f22d64d7010d80cd8016318eecb1da4063123593..4b1d6b9341abe2305919bac320b04a071b2dfc97 100644 --- a/app.js +++ b/app.js @@ -125,6 +125,30 @@ app.post('/login', async (req, res) => { } }); +app.post('/api/products', async (req, res) => { + if (!req.session.user) { + return res.status(401).send('User not logged in'); + } + + const { product_name, product_price, product_img } = req.body; + const userId = req.session.user.id; // ใช้ข้อมูล user จาก session + + if (!product_name || !product_price || !product_img) { + return res.status(400).send('All fields are required'); + } + + try { + // Insert ข้อมูลสินค้าในฐานข้อมูล + const query = 'INSERT INTO products (product_name, product_price, product_img, user_id) VALUES (?, ?, ?, ?)'; + await pool.query(query, [product_name, product_price, product_img, userId]); + + res.status(200).send('Product posted successfully'); + } catch (error) { + console.error('Error adding product:', error); + res.status(500).send('Error posting product'); + } +}); + app.get('/api/getUser', (req, res) => { if (req.session.user) { res.json({ diff --git a/public/post-product.html b/public/post-product.html index 837d34264cba54361ac71dfaf05c13ab2e1fdc0a..5a80f5d2b07c2a3328001423544b486ef500cd52 100644 --- a/public/post-product.html +++ b/public/post-product.html @@ -4,7 +4,7 @@ <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Post a Product</title> - <link rel="stylesheet" href="styles.css"> <!-- คุณสามารถเพิ่ม CSS ตามที่คุณต้องการ --> + <link rel="stylesheet" href="home.css"> <!-- คุณสามารถเพิ่ม CSS ตามที่คุณต้องการ --> </head> <body> <header>