<?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);
$password = filter_input(INPUT_GET, "value", FILTER_SANITIZE_STRING);

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

if ($password == null) {
    $error = 'Password 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 {
    mysqli_query($link, "update `users` set `password` = '$password' where `id` = $id");
    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();
    }
}

