Gitlab@Informatics

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

update-cart

parent 4a5cf9c5
No related branches found
No related tags found
No related merge requests found
...@@ -35,6 +35,7 @@ router.get('/', async (req, res) => { ...@@ -35,6 +35,7 @@ router.get('/', async (req, res) => {
// เพิ่มสินค้าลงตะกร้า // เพิ่มสินค้าลงตะกร้า
router.post('/add', async (req, res) => { router.post('/add', async (req, res) => {
const { productId, quantity } = req.body; const { productId, quantity } = req.body;
console.log("Received:", { productId, quantity });
try { try {
const [[product]] = await pool.query( const [[product]] = await pool.query(
'SELECT stock FROM products WHERE id = ?', [productId] 'SELECT stock FROM products WHERE id = ?', [productId]
...@@ -81,7 +82,6 @@ router.post('/add', async (req, res) => { ...@@ -81,7 +82,6 @@ router.post('/add', async (req, res) => {
// อัพเดทจำนวนสินค้าในตะกร้า // อัพเดทจำนวนสินค้าในตะกร้า
router.post('/update', async (req, res) => { router.post('/update', async (req, res) => {
console.log("Received Data:", req.body);
const { cartItemId, quantity } = req.body; const { cartItemId, quantity } = req.body;
try { try {
const [[cartItem]] = await pool.query( const [[cartItem]] = await pool.query(
......
...@@ -41,17 +41,24 @@ router.get("/add", (req, res) => { ...@@ -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.file:", req.file); // ตรวจสอบว่า multer ได้รับไฟล์หรือไม่
console.log("req.body:", req.body); // ตรวจสอบค่าที่ส่งมาจากฟอร์ม
try { 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; const imagePath = "/uploads/" + req.file.filename;
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, price, stock, description, imagePath] // เพิ่ม description [name, price, stock, description, imagePath]
); );
res.redirect("/products"); res.redirect("/products");
......
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
<th>ชื่อสินค้า</th> <th>ชื่อสินค้า</th>
<th>ราคา</th> <th>ราคา</th>
<th>จำนวน</th> <th>จำนวน</th>
<th>จัดการ</th> <!-- เพิ่มคอลัมน์สำหรับปุ่มแก้ไข --> <th>คำบรรยาย</th>
<th>จัดการ</th>
</tr> </tr>
<% products.forEach(product => { %> <% products.forEach(product => { %>
<tr> <tr>
...@@ -21,6 +22,7 @@ ...@@ -21,6 +22,7 @@
<td><%= product.name %></td> <td><%= product.name %></td>
<td><%= product.price %> บาท</td> <td><%= product.price %> บาท</td>
<td><%= product.stock %></td> <td><%= product.stock %></td>
<td><%= product.description %></td>
</tr> </tr>
<% }); %> <% }); %>
</table> </table>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment