0) { // Verify ownership - check if the campsite belongs to the current user $ownerCheckStmt = $conn->prepare("SELECT user_id FROM campsites WHERE id = ?"); $ownerCheckStmt->bind_param("i", $id); $ownerCheckStmt->execute(); $ownerResult = $ownerCheckStmt->get_result(); if ($ownerResult->num_rows === 0) { http_response_code(404); die('Campsite not found.'); } $ownerRow = $ownerResult->fetch_assoc(); if ($ownerRow['user_id'] != $user_id) { http_response_code(403); die('You do not have permission to edit this campsite. Only the owner can make changes.'); } $ownerCheckStmt->close(); // UPDATE if ($thumbnailPath) { $stmt = $conn->prepare("UPDATE campsites SET name=?, description=?, country=?, province=?, latitude=?, longitude=?, website=?, telephone=?, thumbnail=? WHERE id=?"); $stmt->bind_param("ssssddsssi", $name, $desc, $country, $province, $lat, $lng, $website, $telephone, $thumbnailPath, $id); } else { $stmt = $conn->prepare("UPDATE campsites SET name=?, description=?, country=?, province=?, latitude=?, longitude=?, website=?, telephone=? WHERE id=?"); $stmt->bind_param("ssssddssi", $name, $desc, $country, $province, $lat, $lng, $website, $telephone, $id); } // Log the action auditLog($user_id, 'CAMPSITE_UPDATE', 'campsites', $id, ['name' => $name]); } else { // INSERT $stmt = $conn->prepare("INSERT INTO campsites (name, description, country, province, latitude, longitude, website, telephone, thumbnail, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param("ssssddsssi", $name, $desc, $country, $province, $lat, $lng, $website, $telephone, $thumbnailPath, $user_id); // Log the action auditLog($user_id, 'CAMPSITE_CREATE', 'campsites', 0, ['name' => $name]); } if (!$stmt->execute()) { http_response_code(500); die('Database error: ' . $stmt->error); } $stmt->close(); header("Location: campsites"); ?>