43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'includes/globals.php';
|
|
require_once 'vendor/autoload.php';
|
|
|
|
use DJMixHosting\Database;
|
|
|
|
// Ensure the user is logged in
|
|
if (!isset($_SESSION['user'])) {
|
|
header("Location: login.php");
|
|
exit;
|
|
}
|
|
|
|
// Check that both first and last names were provided
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || empty($_POST['first_name']) || empty($_POST['last_name'])) {
|
|
$_SESSION['error'] = "Both first name and last name are required.";
|
|
header("Location: profile.php");
|
|
exit;
|
|
}
|
|
|
|
$firstName = trim($_POST['first_name']);
|
|
$lastName = trim($_POST['last_name']);
|
|
|
|
$db = new Database($config);
|
|
$userId = $_SESSION['user']['id'];
|
|
|
|
// Update the user's first and last name in the database
|
|
$stmt = $db->prepare("UPDATE users SET firstName = ?, lastName = ? WHERE id = ?");
|
|
$stmt->bind_param("ssi", $firstName, $lastName, $userId);
|
|
if (!$stmt->execute()) {
|
|
$_SESSION['error'] = "Failed to update name. Please try again.";
|
|
header("Location: profile.php");
|
|
exit;
|
|
}
|
|
$stmt->close();
|
|
|
|
// Optionally update session data if you store these fields there
|
|
$_SESSION['user']['firstName'] = $firstName;
|
|
$_SESSION['user']['lastName'] = $lastName;
|
|
|
|
$_SESSION['success'] = "Name updated successfully.";
|
|
header("Location: profile.php");
|
|
exit;
|