> For the complete documentation index, see [llms.txt](https://docs.super-forms.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.super-forms.com/developers/code-examples/compare-input-field-value-with-database-value.md).

# Compare input field value with database value

{% hint style="info" %}
*Insert the below PHP code in your child theme functions.php file, or create a custom plugin. You may also use a plugin that allows you to insert code sinppets to your site.*
{% endhint %}

In this example we validate our form field named `code` with our database codes list. The database name is `db_name` and table name is `wp_codes`. The column is named `code` which we use for our comparison.

When the code entered by the user does not exists, we will display an error message to the user, and the form will not be submitted.

```php
add_action( 'super_before_sending_email_hook', 'f4d_compare_database_value', 1 );
function f4d_compare_database_value($x){
    extract(shortcode_atts(array('data'=>array(), 'form_id'=>0), $x));
    $your_form_id = 12345;
    if($form_id!==$your_form_id) return;
    $data = wp_unslash($data);
    // Our form has a field named `code` and we will lookup if this code exists in our custom databaset table named `wp_codes`
    // Make sure to trim whitespaces at the start and end of the string
    $code = trim(sanitize_text_field($data['code']['value']));
    // Table structure that is used in this example:
    // CREATE TABLE `db_name`.`wp_codes` (`id` INT NOT NULL AUTO_INCREMENT , `code` VARCHAR(50) NOT NULL , PRIMARY KEY (`id`), UNIQUE (`code`)) ENGINE = InnoDB;
    global $wpdb;
    $codesTable = $wpdb->prefix.'codes';
    $sql = $wpdb->prepare("SELECT COUNT(*) FROM $codesTable AS c WHERE c.code = '%s'", $code);
    $found = $wpdb->get_var($sql);
    if(absint($found)===0){
        // Code does not exists, return error
        echo json_encode(
            array(
                'error' => true,
                'fields' => array('code'), // field that contains errors
                'msg' => 'Entered code is invalid, please try again'
            )
        );
        die();
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.super-forms.com/developers/code-examples/compare-input-field-value-with-database-value.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
