How can PHP session variables be used to prevent multiple database updates when a button is clicked multiple times?

To prevent multiple database updates when a button is clicked multiple times, we can use PHP session variables to track whether the update has already been processed. We can set a session variable when the update is done and check this variable before performing the update again.

<?php
session_start();

if(isset($_POST['update_button'])) {
    if(!isset($_SESSION['update_done'])) {
        // Perform database update here
        
        // Set session variable to indicate update has been done
        $_SESSION['update_done'] = true;
    } else {
        // Update already done, do not perform again
    }
}
?>