gravity forms validate field entry values against mysql table value

Basically, I have a table with 8 million codes for promotion. The contestant will enter the code on a gravity form, which will then query the database for code, once it is there the code will be marked as used, and the contestant should be redirected to the entry form. If not the form should tell the user code is invalid. This is my code below which I pulled from other sources because I am not a coder. However, when I submit, the correct code, it says incorrect. Any help will be appreciated just bear in mind I am not a programmer

add_filter( 'gform_field_validation_4', 'validate_code' );
function validate_code($validation_result){
    $form = $validation_result['form'];
    foreach( $form['fields'] as &$field ) {
    if ( strpos( $field->cssClass, 'validate-code' ) === false ) {
    $field_value = rgpost( "input_1" );
    $is_valid = is_code( $field_value );
    $validation_result['is_valid'] = false;
    $field->failed_validation = true;
    $field->validation_message = 'The code you have entered is not valid. Please enter another.';
    $validation_result['form'] = $form;
    return $validation_result;
function is_code($code){
    $info = 'mysql response';
require_once(ABSPATH . '/wp-config.php');
    $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        echo 'Unable to connect to server, please use the back button in your browser and resubmit the form';
    $code = mysqli_real_escape_string($conn, $_POST['codes']);
    $sql_check = mysqli_query($conn, "SELECT * FROM entrycode WHERE codes = '$code' AND valid = 0");
    $sql_checkrows = mysqli_num_rows($sql_check);
    if ($sql_checkrows > 0) {
        $sql_update = "UPDATE entrycode SET used = 1 WHERE codes ='$code'";
        if (mysqli_query($conn, $sql_update)) {
            $info = mysqli_affected_rows($conn) . " Row updated.\n";
            $info = 'success';
            return true;
    } else {
        return false;
add_filter('gform_confirmation_4', 'valid_invitation_confirmation', 10, 4);
function valid_invitation_confirmation($confirmation, $form, $lead, $ajax){
    // customize this URL - I send the code in the query string
    $success_url = '' . '/?code=' . $lead[1];
    $confirmation = array('redirect' => $success_url);
    return $confirmation;
How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum