prepare("SELECT bio, specialties FROM coaches WHERE id = ?"); $stmt->execute([$coach_id]); $coach = $stmt->fetch(); $bio = $coach['bio'] ?? ''; $specialties = $coach['specialties'] ?? ''; // Handle form submission for bio and specialties if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_profile'])) { $bio = trim($_POST['bio']); $specialties = trim($_POST['specialties']); $stmt = $pdo->prepare("UPDATE coaches SET bio = ?, specialties = ? WHERE id = ?"); $stmt->execute([$bio, $specialties, $coach_id]); header('Location: edit-portfolio.php?success=1'); exit(); } // Handle media upload if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['upload_media'])) { if (isset($_FILES['media']) && $_FILES['media']['error'] == 0) { $caption = trim($_POST['caption']); $allowed = ['jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif']; $filename = $_FILES['media']['name']; $filetype = $_FILES['media']['type']; $filesize = $_FILES['media']['size']; $ext = pathinfo($filename, PATHINFO_EXTENSION); if (!array_key_exists($ext, $allowed)) { die("Error: Please select a valid file format."); } $maxsize = 5 * 1024 * 1024; if ($filesize > $maxsize) { die("Error: File size is larger than the allowed limit."); } if (in_array($filetype, $allowed)) { $new_filename = uniqid() . '.' . $ext; $filepath = 'uploads/portfolio/' . $new_filename; $error_message = ''; if (move_uploaded_file($_FILES['media']['tmp_name'], $filepath)) { $stmt = $pdo->prepare("INSERT INTO coach_portfolio_items (coach_id, item_type, url, caption) VALUES (?, 'image', ?, ?)"); $stmt->execute([$coach_id, $filepath, $caption]); header('Location: edit-portfolio.php?success=2'); exit(); } else { $error_message = 'Error: There was a problem uploading your file. Please try again.'; } } } } // Handle media deletion if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_media'])) { $item_id = $_POST['delete_item_id']; // First, get the file path to delete the file $stmt = $pdo->prepare("SELECT url FROM coach_portfolio_items WHERE id = ? AND coach_id = ?"); $stmt->execute([$item_id, $coach_id]); $item = $stmt->fetch(); if ($item) { // Delete file from server if (file_exists($item['url'])) { unlink($item['url']); } // Delete from database $stmt = $pdo->prepare("DELETE FROM coach_portfolio_items WHERE id = ?"); $stmt->execute([$item_id]); header('Location: edit-portfolio.php?success=3'); exit(); } } // Fetch all portfolio items for the coach $stmt = $pdo->prepare("SELECT * FROM coach_portfolio_items WHERE coach_id = ? ORDER BY created_at DESC"); $stmt->execute([$coach_id]); $portfolio_items = $stmt->fetchAll(); ?>