What are some best practices for displaying error messages when form input does not match database values in PHP?

When form input does not match database values in PHP, it is important to display clear and specific error messages to the user. One best practice is to validate the form input against the database before processing it, and if a mismatch is found, display an error message indicating the issue to the user.

// Assuming $input is the form input and $db_values is an array of database values
$input = $_POST['input']; // Retrieve form input
$db_values = ['value1', 'value2', 'value3']; // Example database values

// Validate form input against database values
if (!in_array($input, $db_values)) {
    // Display error message to the user
    echo "Error: Input does not match database values.";
}