36780-vm/admin/product_update.php
Flatlogic Bot 6859a96e73 v2
2025-12-09 09:59:27 +00:00

89 lines
3.3 KiB
PHP

<?php
require_once '../db/config.php';
// Check if it's a POST request
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: products.php');
exit;
}
// Basic validation
$required_fields = ['id', 'name', 'price', 'duration', 'description'];
foreach ($required_fields as $field) {
if (empty($_POST[$field])) {
header('Location: product_edit.php?id=' . $_POST['id'] . '&status=error&message=Semua field wajib diisi.');
exit;
}
}
$product_id = $_POST['id'];
$name = $_POST['name'];
$price = $_POST['price'];
$duration = $_POST['duration'];
$description = $_POST['description'];
$features = $_POST['features'] ?? '';
$thumbnail_path = null;
try {
$pdo = db();
// First, get the current thumbnail path
$stmt = $pdo->prepare("SELECT thumbnail_url FROM products WHERE id = ?");
$stmt->execute([$product_id]);
$current_thumbnail = $stmt->fetchColumn();
// Handle file upload
if (isset($_FILES['thumbnail']) && $_FILES['thumbnail']['error'] == UPLOAD_ERR_OK) {
$upload_dir = '../assets/uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
$filename = uniqid() . '-' . basename($_FILES['thumbnail']['name']);
$target_file = $upload_dir . $filename;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Basic validation for image
$check = getimagesize($_FILES['thumbnail']['tmp_name']);
if ($check === false) {
header('Location: product_edit.php?id=' . $product_id . '&status=error&message=File bukan gambar.');
exit;
}
if (!in_array($imageFileType, ['jpg', 'png', 'jpeg', 'gif'])) {
header('Location: product_edit.php?id=' . $product_id . '&status=error&message=Hanya format JPG, JPEG, PNG & GIF yang diperbolehkan.');
exit;
}
if (move_uploaded_file($_FILES['thumbnail']['tmp_name'], $target_file)) {
$thumbnail_path = 'assets/uploads/' . $filename;
// Delete the old thumbnail if a new one is uploaded
if ($current_thumbnail && file_exists('../' . $current_thumbnail)) {
unlink('../' . $current_thumbnail);
}
} else {
header('Location: product_edit.php?id=' . $product_id . '&status=error&message=Gagal mengunggah thumbnail.');
exit;
}
} else {
// Keep the old thumbnail if no new one is uploaded
$thumbnail_path = $current_thumbnail;
}
// Update data in the database
$sql = "UPDATE products SET name = ?, price = ?, duration = ?, description = ?, features = ?, thumbnail_url = ? WHERE id = ?";
$stmt = $pdo->prepare($sql);
if ($stmt->execute([$name, $price, $duration, $description, $features, $thumbnail_path, $product_id])) {
header('Location: products.php?status=success&message=Produk berhasil diperbarui.');
} else {
throw new Exception("Gagal memperbarui produk di database.");
}
} catch (PDOException $e) {
// Redirect with a generic error
header('Location: product_edit.php?id=' . $product_id . '&status=error&message=Database error: ' . $e->getMessage());
} catch (Exception $e) {
header('Location: product_edit.php?id=' . $product_id . '&status=error&message=' . $e->getMessage());
}
exit;