0) { $target_dir = "../uploads/img-barang/"; if (!file_exists($target_dir)) { mkdir($target_dir, 0777, true); } // Use the original filename $filename = $_FILES["product_image"]["name"]; $target_file = $target_dir . $filename; // Check if image file is actual image $check = getimagesize($_FILES["product_image"]["tmp_name"]); if ($check !== false) { // Delete old image if it exists if (!empty($gambar)) { $old_file = $target_dir . $gambar; if (file_exists($old_file)) { unlink($old_file); // Delete the old image file } } // Upload file if (move_uploaded_file($_FILES["product_image"]["tmp_name"], $target_file)) { $gambar = $filename; } } } // Update query $sql = "UPDATE barang_kasir SET nama_barang = ?, id_jenis = ?, stok = ?, harga = ?, gambar = ? WHERE kode_barang = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("siiiss", $nama_barang, $id_jenis, $stok, $harga, $gambar, $kode_barang); if ($stmt->execute()) { header("Location: /ayula-store/views/barang-kasir/?edit_success=1"); exit(); } else { $error = "Failed to update product"; } } // Get product ID from URL $kode_barang = isset($_GET['id']) ? $_GET['id'] : 0; // Fetch product details $sql = "SELECT * FROM barang_kasir WHERE kode_barang = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("s", $kode_barang); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { $product = $result->fetch_assoc(); } else { header("Location: /ayula-store/views/barang-kasir/?error=Product not found"); exit(); } // Get all categories $sql_categories = "SELECT * FROM jenis_barang ORDER BY nama_jenis"; $result_categories = $conn->query($sql_categories); $categories = []; if ($result_categories->num_rows > 0) { while ($row = $result_categories->fetch_assoc()) { $categories[] = $row; } } ?>