prepare("SELECT * FROM drills WHERE id = ? AND coach_id = ?"); $stmt->execute([$drill_id, $current_coach_id]); $drill = $stmt->fetch(PDO::FETCH_ASSOC); if (!$drill) { header('Location: my_drills.php?error=Drill not found or you do not have permission to edit it'); exit(); } // Fetch current categories for the drill $stmt = $pdo->prepare("SELECT category_id FROM drill_categories WHERE drill_id = ?"); $stmt->execute([$drill_id]); $current_category_ids = $stmt->fetchAll(PDO::FETCH_COLUMN); } catch (PDOException $e) { error_log("Database error: " . $e->getMessage()); header('Location: my_drills.php?error=A database error occurred.'); exit(); } $pageTitle = 'Edit Drill'; $pageDescription = 'Update the details of your football/soccer drill.'; $errors = []; // Define selectable options $age_groups = ['U6-U8', 'U9-U12', 'U13-U16', 'U17+', 'Adults']; $skill_focuses = ['Dribbling', 'Passing', 'Shooting', 'Defense', 'Goalkeeping', 'Crossing', 'Finishing', 'First Touch']; $difficulties = ['Beginner', 'Intermediate', 'Advanced']; // Fetch all categories $stmt = $pdo->query("SELECT * FROM categories ORDER BY name"); $categories = $stmt->fetchAll(PDO::FETCH_ASSOC); if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Sanitize and validate inputs $title = trim($_POST['title'] ?? ''); $description = trim($_POST['description'] ?? ''); $min_players = filter_input(INPUT_POST, 'min_players', FILTER_VALIDATE_INT); $max_players = filter_input(INPUT_POST, 'max_players', FILTER_VALIDATE_INT); $age_group = trim($_POST['age_group'] ?? ''); $skill_focus = trim($_POST['skill_focus'] ?? ''); $difficulty = trim($_POST['difficulty'] ?? ''); $duration_minutes = filter_input(INPUT_POST, 'duration_minutes', FILTER_VALIDATE_INT); $equipment_required = trim($_POST['equipment_required'] ?? ''); $youtube_url = filter_input(INPUT_POST, 'youtube_url', FILTER_VALIDATE_URL); $is_public = isset($_POST['is_public']) ? 1 : 0; $selected_categories = $_POST['categories'] ?? []; $image_path = $drill['image_path']; // Keep old image by default // Basic validation if (empty($title)) $errors[] = 'Title is required.'; if (empty($description)) $errors[] = 'Description is required.'; if ($min_players === false || $min_players < 1) $errors[] = 'Minimum players must be a positive number.'; if ($max_players === false || $max_players < $min_players) $errors[] = 'Maximum players must be greater than or equal to minimum players.'; if (!in_array($age_group, $age_groups)) $errors[] = 'Invalid age group selected.'; if (!in_array($skill_focus, $skill_focuses)) $errors[] = 'Invalid skill focus selected.'; if (!in_array($difficulty, $difficulties)) $errors[] = 'Invalid difficulty selected.'; if ($duration_minutes === false || $duration_minutes < 1) $errors[] = 'Duration must be a positive number.'; if ($youtube_url === false && !empty($_POST['youtube_url'])) $errors[] = 'YouTube URL is not a valid URL.'; if (empty($selected_categories)) $errors[] = 'At least one category must be selected.'; // New image upload handling if (isset($_FILES['drill_image']) && $_FILES['drill_image']['error'] === UPLOAD_ERR_OK) { $upload_dir = __DIR__ . '/assets/images/drills/'; if (!is_dir($upload_dir)) { mkdir($upload_dir, 0775, true); } $file = $_FILES['drill_image']; $file_ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); $allowed_exts = ['jpg', 'jpeg', 'png', 'gif']; if (in_array($file_ext, $allowed_exts)) { if ($file['size'] <= 5 * 1024 * 1024) { // 5MB limit $new_filename = uniqid('', true) . '.' . $file_ext; $destination = $upload_dir . $new_filename; if (move_uploaded_file($file['tmp_name'], $destination)) { // Delete old image if it exists if ($drill['image_path'] && file_exists(__DIR__ . '/' . $drill['image_path'])) { unlink(__DIR__ . '/' . $drill['image_path']); } $image_path = '/assets/images/drills/' . $new_filename; } else { $errors[] = 'Failed to move uploaded file.'; } } else { $errors[] = 'File is too large. Maximum size is 5MB.'; } } else { $errors[] = 'Invalid file type. Only JPG, JPEG, PNG, and GIF are allowed.'; } } if (empty($errors)) { try { $pdo->beginTransaction(); $stmt = $pdo->prepare( 'UPDATE drills SET title = ?, description = ?, min_players = ?, max_players = ?, age_group = ?, skill_focus = ?, difficulty = ?, duration_minutes = ?, equipment_required = ?, youtube_url = ?, is_public = ?, image_path = ? WHERE id = ? AND coach_id = ?' ); $stmt->execute([ $title, $description, $min_players, $max_players, $age_group, $skill_focus, $difficulty, $duration_minutes, $equipment_required, $youtube_url ?: null, $is_public, $image_path, $drill_id, $current_coach_id ]); // Update categories // 1. Delete existing categories for this drill $stmt = $pdo->prepare("DELETE FROM drill_categories WHERE drill_id = ?"); $stmt->execute([$drill_id]); // 2. Insert new categories $stmt = $pdo->prepare('INSERT INTO drill_categories (drill_id, category_id) VALUES (?, ?)'); foreach ($selected_categories as $category_id) { $stmt->execute([$drill_id, $category_id]); } $pdo->commit(); header("Location: my_drills.php?success=Drill updated successfully"); exit; } catch (PDOException $e) { $pdo->rollBack(); // In a real app, log this error instead of displaying it $errors[] = "Database error: " . $e->getMessage(); } } } ?>

Edit Drill

Update the details of your drill.

Please fix the following errors:

Current Drill Image
Upload a new image to replace the current one (JPG, PNG, GIF - max 5MB).
Select one or more categories (hold Ctrl or Cmd to select multiple).
>