How to code Conditional Logic in multipage form matching values stored in Custom DB Table?

Hello, I hope somebody could help me.
First of all I made a custom Database Table named wp367_community where I previously stored a set of data in these cells: IDentityCode, HealthCard, FirstName, LastName, Gender, Born.
After that I made a gravity multipage form….
In page 1 I’ve created two fields where the user could enter his IDentityCode and his HealthCard values…
But now I don’t know how to check if IdentityCode and Healthcare values entered by the user from the form are already stored in the custom DB table wp367_community; I need this control condition to match the two couples of values to go on second page …
So I need to know how to code the conditional snippet on functions.php to proceed on page 2 where to take the IdentityCode value .
I think the best thing is to use the gform_field_validation filter…
The form_id is 9 and the IDentityCode and HealthCard on the form have respectively id 1 and 2.
So probably the snippet code looks something like this:

add_filter( 'gform_field_validation_9', 'matching_function', 10, 4 );
function matching_function() {
    $IDentityCode_value = $_POST['input_1'];
    $HealthCard_value = $_POST['input_2'];
    global $wpdb;
    $matching_count = $wpdb->get_var( "SELECT COUNT(*) FROM wp367_community WHERE IDentityCode=$IDentityCode_value AND HealthCard=$HealthCard_value" );  
    if ( $matching_count == 1  ) {
        … go to second page keeping on it $IDentityCode_value …
    } elseif ( $matching_count == 0  ) {
    echo "The values don't match! Please retry.";
    }
  }

Please, could someone drive me on the right way? thank you.
Regards, Paolo.