<?php
session_start();

if (!isset($_SESSION['username'], $_SESSION['userid'], $_SESSION['database'])) {
    header("Location: login.html");
    exit();
}

$username = $_SESSION['username'];
$userid = $_SESSION['userid'];
$database = $_SESSION['database'];


$response = ['status'=>''];

$uname = $_POST['uname'] ?? '';
$inputUsername = $_POST['username'] ?? '';
$email = $_POST['email'] ?? '';
$country = $_POST['country'] ?? '';
$phone = $_POST['phone'] ?? '';
$countryCode = $_POST['countryCode'] ?? '';

try {
    require_once 'connection.php';

    $stmt = $pdo->prepare("UPDATE USERS SET UNAME=:uname, USERNAME=:inputUsername, CONTACT=:phone, COUNTRYCODE=:countryCode WHERE USERNAME=:username AND USERID=:userid");
    $stmt->bindParam(':uname', $uname);
    $stmt->bindParam(':inputUsername', $inputUsername);
    $stmt->bindParam(':phone', $phone);
    $stmt->bindParam(':countryCode', $countryCode);
    $stmt->bindParam(':username', $username);
    $stmt->bindParam(':userid', $userid);
    $stmt->execute();
    $response['status'] = 'Success';
} catch (PDOException $e) {
    $response['status'] = 'Failure';
}
echo json_encode($response);