prepare( "UPDATE users SET display_name = ?, description = ?, occupation = ?, city = ?, country = ? WHERE id = ?" ); $stmt->execute([$displayName, $description, $occupation, $city, $country, $userId]); // Handle skills $skills = json_decode($skillsJson, true); if (is_array($skills) && !empty($skills)) { // First, remove existing skills for the user to prevent duplicates on re-editing $deleteStmt = $pdo->prepare("DELETE FROM skills WHERE user_id = ?"); $deleteStmt->execute([$userId]); // Insert new skills $insertStmt = $pdo->prepare("INSERT INTO skills (user_id, skill_name) VALUES (?, ?)"); foreach ($skills as $skill) { if (!empty(trim($skill))) { $insertStmt->execute([$userId, trim($skill)]); } } } set_flash_message('Profile updated successfully!', 'success'); // Redirect to a success page or the main feed header('Location: profile.php?id=' . $userId); exit(); } catch (PDOException $e) { set_flash_message('Database error: ' . $e->getMessage(), 'danger'); header('Location: profile_setup.php'); exit(); } } else { // If not a POST request, redirect away header('Location: profile_setup.php'); exit(); } ?>