<?php

require_once '../../vendor/autoload.php';
require_once '../../include/DatabaseConn.php';

$db = new DatabaseConn();
$link = $db->connect();

$std = new stdClass();

$id = filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT);
$phone = filter_input(INPUT_GET, "value", FILTER_SANITIZE_STRING);

header("content-type: application/json");

if ($phone == null) {
    $error = 'Address is not valid.';
    $std->error = $error;
    echo (json_encode($std));
    die();
} else if (is_null($id)) {
    $error = 'Token missing in request body';
    $std->error = $error;
    echo (json_encode($std));
    die();
} else {
    $res = mysqli_query($link, "select email from users where id = $id");
    $user = mysqli_fetch_assoc($res);
    $user = $user['email'];
    mysqli_query($link, "update `address` set `address` = '$phone' where `user` = '$user'");
    if (mysqli_affected_rows($link) == 1) {
        $std->msg = "Operation was successful";
        echo (json_encode($std));
        die();
    } else {
        $error = 'Already updated';
        $std->error = $error;
        echo (json_encode($std));
        die();
    }
}

