<?php
$data = json_decode(file_get_contents('php://input'));
$email = $data->username;
$phone = $data->phone;
$address = $data->street;
$name = $data->city;
$plz = $data->postalcode;
$password = $data->password;
$hashedPassword = $data->hashedPassword;
updateData($email, $phone, $address, $name, $plz, $password, $hashedPassword);

function updateData($email, $phone, $address, $name, $plz, $password, $hashedPassword){
    require($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php');
    if (password_verify($password, $hashedPassword)) {
        $sqlOrt = "INSERT IGNORE INTO `cities` (`postalcode`, `name`) VALUES (?, ?)";
        $sqlUser = "UPDATE `user` SET `phone` = ?, `address` = ?, `plz` = ? WHERE `username` = ?";
        $stmtOrt = $db->prepare($sqlOrt);
        $stmtUser = $db->prepare($sqlUser);
        $stmtOrt->bind_param("ss", $plz, $name);
        $stmtUser->bind_param("ssss", $phone, $address, $plz, $email);

        if ($stmtOrt->execute() && $stmtUser->execute()) {
            $response = "success";
        } else {
            $response = "failure";
        }
        $stmtOrt->close();
        $stmtUser->close();
        $db->close();
    } else {
        $response = "failure";
    }
    
    echo $response;


}