<?php
require 'database.php';


if(isset($_POST['token']) && isset($_SESSION['user_id'])) {
    $token = mysqli_real_escape_string($con, $_POST['token']);
    $user_id = $_SESSION['user_id'];

    // This query inserts the token or updates it if it already exists
    $query = "INSERT INTO user_tokens (user_id, device_token) VALUES ('$user_id', '$token') 
              ON DUPLICATE KEY UPDATE device_token = '$token'";
    
    mysqli_query($con, $query);
    echo "success";
}
?>