prepare("SELECT COUNT(*) FROM cvs WHERE user_id = ?"); $stmt->execute([$user_id]); $cv_count = $stmt->fetchColumn(); if ($cv_count >= 2) { // Limit reached for free users $_SESSION['error_message'] = 'You have reached the maximum of 2 CVs for the Free Plan. Please upgrade to create more.'; header('Location: /dashboard.php'); exit; } } $content = json_encode([ 'personal_info' => $_POST['personal_info'] ?? [], 'experience' => array_values($_POST['experience'] ?? []), 'education' => array_values($_POST['education'] ?? []), 'skills' => $_POST['skills'] ?? '' ]); if ($cv_id) { // Update existing CV $stmt = $pdo->prepare('UPDATE cvs SET title = :title, content = :content, template_id = :template_id, updated_at = NOW() WHERE id = :id AND user_id = :user_id'); $stmt->execute([ 'id' => $cv_id, 'user_id' => $user_id, 'title' => $title, 'content' => $content, 'template_id' => $template_id ]); } else { // Insert new CV $stmt = $pdo->prepare('INSERT INTO cvs (user_id, title, content, template_id) VALUES (:user_id, :title, :content, :template_id)'); $stmt->execute([ 'user_id' => $user_id, 'title' => $title, 'content' => $content, 'template_id' => $template_id ]); } header('Location: /dashboard.php'); exit; } else { // Not a POST request header('Location: /dashboard.php'); exit; }