36318-vm/handle_profile_setup.php
Flatlogic Bot 5274c73966 Base app
2025-11-26 13:53:30 +00:00

71 lines
2.2 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
require_once 'db/config.php';
require_once 'includes/flash_messages.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$userId = $_SESSION['user_id'];
$displayName = trim($_POST['display_name'] ?? '');
$description = trim($_POST['description'] ?? '');
$occupation = trim($_POST['occupation'] ?? '');
$skillsJson = $_POST['skills'] ?? '[]';
$city = trim($_POST['city'] ?? '');
$country = trim($_POST['country'] ?? '');
// Validate display name
if (empty($displayName)) {
set_flash_message('Display Name is required.', 'danger');
header('Location: profile_setup.php');
exit();
}
try {
$pdo = db();
// Update users table
$stmt = $pdo->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();
}
?>