query("DELETE FROM products WHERE id = $id"); header("Location: products.php"); } // Handle Add/Edit $edit_prod = null; if(isset($_GET['edit'])) { $id = intval($_GET['edit']); $edit_prod = $conn->query("SELECT * FROM products WHERE id = $id")->fetch_assoc(); } if(isset($_POST['save_product'])) { $name = $_POST['name']; $cat_id = $_POST['category_id']; $price = $_POST['price']; $old_price = $_POST['old_price']; $discount = $_POST['discount']; $desc = $_POST['description']; $image = $_POST['existing_image']; if(!empty($_FILES['image']['name'])) { $image = time() . "_" . $_FILES['image']['name']; move_uploaded_file($_FILES['image']['tmp_name'], "../uploads/" . $image); } if(isset($_POST['product_id']) && !empty($_POST['product_id'])) { $id = $_POST['product_id']; $stmt = $conn->prepare("UPDATE products SET name=?, category_id=?, price=?, old_price=?, discount=?, image=?, description=? WHERE id=?"); $stmt->bind_param("siddissi", $name, $cat_id, $price, $old_price, $discount, $image, $desc, $id); } else { $stmt = $conn->prepare("INSERT INTO products (name, category_id, price, old_price, discount, image, description) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param("siddiss", $name, $cat_id, $price, $old_price, $discount, $image, $desc); } $stmt->execute(); header("Location: products.php"); } ?>

Manage Products

query("SELECT p.*, c.name as cat_name FROM products p JOIN categories c ON p.category_id = c.id ORDER BY p.id DESC"); while($row = $res->fetch_assoc()): ?>
ID Image Name Category Price Discount Action
Rs. %