Gitlab@Informatics

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

update-cart

parent 4c727541
Branches
No related tags found
No related merge requests found
......@@ -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;
......@@ -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>
<% }); %>
......
......@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment