How can AJAX be utilized to update database records in PHP without reloading the entire page?

To update database records in PHP without reloading the entire page using AJAX, you can create a PHP script that handles the database update operation and then use JavaScript to send an AJAX request to this script. The PHP script will update the database records based on the data sent in the AJAX request, and then return a response to the JavaScript function indicating the success or failure of the update.

<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get data from AJAX request
$data = json_decode(file_get_contents("php://input"));

// Update database record
$sql = "UPDATE table_name SET column1 = '{$data->value1}', column2 = '{$data->value2}' WHERE id = '{$data->id}'";

if ($conn->query($sql) === TRUE) {
    echo json_encode(array("status" => "success"));
} else {
    echo json_encode(array("status" => "error"));
}

$conn->close();
?>