prepare("SELECT id FROM destinations WHERE name = ?"); $check->bind_param("s", $name); $check->execute(); $check->store_result(); if ($check->num_rows > 0) { $error = "A destination named \"$name\" already exists."; } else { // Handle image upload $image_filename = ''; if (!empty($_FILES['image']['name'])) { $allowed = ['jpg','jpeg','png','webp','gif']; $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION)); if (!in_array($ext, $allowed)) { $error = 'Invalid image type. Allowed: jpg, jpeg, png, webp, gif'; } elseif ($_FILES['image']['size'] > 5 * 1024 * 1024) { $error = 'Image must be under 5 MB.'; } else { $image_filename = 'dest_' . time() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '_', $_FILES['image']['name']); $upload_path = '../uploads/' . $image_filename; if (!move_uploaded_file($_FILES['image']['tmp_name'], $upload_path)) { $error = 'Failed to upload image.'; $image_filename = ''; } } } if (empty($error)) { $stmt = $conn->prepare("INSERT INTO destinations (name, country_code, description, image, show_home) VALUES (?, ?, ?, ?, ?)"); if ($stmt) { $stmt->bind_param("ssssi", $name, $country_code, $description, $image_filename, $show_home); if ($stmt->execute()) { header("Location: destinations.php?msg=saved"); exit; } else { $error = 'Database error: ' . $stmt->error; } } else { // Fallback for tables without country_code/description/show_home columns $stmt2 = $conn->prepare("INSERT INTO destinations (name, image) VALUES (?, ?)"); if ($stmt2) { $stmt2->bind_param("ss", $name, $image_filename); if ($stmt2->execute()) { header("Location: destinations.php?msg=saved"); exit; } else { $error = 'Database error: ' . $stmt2->error; } } else { $error = 'Database prepare error: ' . $conn->error; } } } } } } ?>