How can one prevent users from resubmitting or altering values in a database when using a button on a PHP page?

To prevent users from resubmitting or altering values in a database when using a button on a PHP page, you can implement a form token system. This involves generating a unique token when the form is loaded and checking if the token matches when the form is submitted. If the tokens do not match, the submission should be rejected.

<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (!isset($_POST['token']) || $_POST['token'] !== $_SESSION['token']) {
        die("Invalid form submission");
    }

    // Process form submission
}

$token = md5(uniqid(rand(), true));
$_SESSION['token'] = $token;
?>

<form method="post" action="">
    <input type="hidden" name="token" value="<?php echo $token; ?>">
    <!-- Other form fields -->
    <button type="submit">Submit</button>
</form>