<?php
include 'config.php';
if(!isset($_SESSION['user_id'])) exit();

$user_id = $_SESSION['user_id'];
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];

if(!empty($password)) {
    $hashed_pass = password_hash($password, PASSWORD_DEFAULT);
    $stmt = $conn->prepare("UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?");
    $stmt->bind_param("sssi", $name, $email, $hashed_pass, $user_id);
} else {
    $stmt = $conn->prepare("UPDATE users SET name = ?, email = ? WHERE id = ?");
    $stmt->bind_param("ssi", $name, $email, $user_id);
}

if($stmt->execute()) {
    $_SESSION['user_name'] = $name;
    header("Location: profile.php?msg=updated");
} else {
    echo "Error updating profile.";
}
?>