connect_error) { die("Connection failed: " . $conn->connect_error); } return $conn; } // Get product by ID function getProductById($id) { $conn = dbConnect(); $sql = "SELECT * FROM barang WHERE id_barang = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $id); $stmt->execute(); $result = $stmt->get_result(); $product = $result->fetch_assoc(); $stmt->close(); $conn->close(); return $product; } // Get all product categories function getProductCategories() { $conn = dbConnect(); $sql = "SELECT * FROM jenis_barang ORDER BY nama_jenis"; $result = $conn->query($sql); $categories = []; if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $categories[] = $row; } } $conn->close(); return $categories; } //fungsi update product function updateProduct($id, $data) { $conn = dbConnect(); $sql = "UPDATE barang SET nama_barang = ?, harga = ?, stok = ?, id_jenis = ?, image = ? WHERE id_barang = ?"; $stmt = $conn->prepare($sql); // Change from "ssdiis" to "ssdisi" - notice the 'i' to 's' change for image parameter $stmt->bind_param("ssdisi", $data['nama_barang'], $data['harga'], $data['stok'], $data['id_jenis'], $data['image'], $id ); $result = $stmt->execute(); $stmt->close(); $conn->close(); return $result; } // Check if form is submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { $id = $_POST['id_barang']; // Handle image upload if a new file is selected $image = $_POST['current_image']; // Default to current image if(isset($_FILES['product_image']) && $_FILES['product_image']['size'] > 0) { $target_dir = "image/"; $file_extension = strtolower(pathinfo($_FILES["product_image"]["name"], PATHINFO_EXTENSION)); $new_filename = "product_" . time() . "." . $file_extension; $target_file = $target_dir . $new_filename; // Check if image file is an actual image $check = getimagesize($_FILES["product_image"]["tmp_name"]); if($check !== false) { // Upload file if (move_uploaded_file($_FILES["product_image"]["tmp_name"], $target_file)) { $image = $new_filename; } } } // Prepare data for update $data = [ 'nama_barang' => $_POST['nama_barang'], 'harga' => $_POST['harga'], 'stok' => $_POST['stok'], 'id_jenis' => $_POST['id_jenis'], 'image' => $image ]; // Update product if(updateProduct($id, $data)) { // Redirect to product list with success message header("Location: productlist.php?success=Product updated successfully"); exit(); } else { $error = "Failed to update product"; } } // Get product ID from URL $product_id = isset($_GET['id']) ? $_GET['id'] : 0; $product = getProductById($product_id); // Get all categories $categories = getProductCategories(); // If product doesn't exist, redirect to product list if (!$product) { header("Location: productlist.php?error=Product not found"); exit(); } ?> Ayula Store - Edit Product
img

Pilih Gambar

  • img

Batal