From ea31277fe82c45c4ba3854c1ade85d0944a9350e Mon Sep 17 00:00:00 2001 From: 65160270 <65160270@go.buu.ac.th> Date: Fri, 28 Mar 2025 23:31:21 +0700 Subject: [PATCH] update-cart --- shop-routes/product.js | 16 ++++++++++++++++ views/index.ejs | 8 +++++++- views/product.ejs | 7 +++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/shop-routes/product.js b/shop-routes/product.js index 92b6503..0511268 100644 --- a/shop-routes/product.js +++ b/shop-routes/product.js @@ -111,5 +111,21 @@ router.post("/edit/:id", upload.single("image"), async (req, res) => { } }); +// เธฅเธเธชเธดเธเธเนเธฒ +router.post("/delete/:id", async (req, res) => { + try { + const productId = req.params.id; + + // เธฅเธเธชเธดเธเธเนเธฒเธญเธญเธเธเธฒเธเธเธฒเธเธเนเธญเธกเธนเธฅ + await pool.execute("DELETE FROM products WHERE id = ?", [productId]); + + console.log("Product deleted successfully!"); + res.redirect("/products"); // เธเธฅเธฑเธเนเธเธขเธฑเธเธซเธเนเธฒเธฃเธฒเธขเธเธฒเธฃเธชเธดเธเธเนเธฒ + } catch (error) { + console.error("Error deleting product:", error); + res.status(500).send("Error deleting product."); + } +}); + module.exports = router; diff --git a/views/index.ejs b/views/index.ejs index ee1007c..ee37dee 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -20,7 +20,13 @@ <button type="submit">Add to Cart</button> </form> <td> - <a href="/products/edit/<%= product.id %>">Edit</a> <!-- Edit button --> + <a href="/products/edit/<%= product.id %>">Edit</a> + + <form action="/products/delete/<%= product.id %>" method="POST" style="display: inline;"> + <button type="submit" onclick="return confirm('Are you sure you want to delete this product?')"> + Delete + </button> + </form> </td> </div> <% }); %> diff --git a/views/product.ejs b/views/product.ejs index 4692966..01f834c 100644 --- a/views/product.ejs +++ b/views/product.ejs @@ -22,6 +22,13 @@ <td><%= product.price %> THB</td> <td><%= product.stock %></td> <td><%= product.description %></td> + <td> + <form action="/products/delete/<%= product.id %>" method="POST" style="display:inline;"> + <button type="submit" onclick="return confirm('Are you sure you want to delete this product?')"> + Delete + </button> + </form> + </td> </tr> <% }); %> </table> -- GitLab