# Validate Code Entry vs MySQL DB

**URL:** <https://community.gravityforms.com/t/validate-code-entry-vs-mysql-db/1893>\
**Category:** Get Help\
**Tags:** gform\_field\_validation, mysql, gform\_validation\
**Created:** [May 20, 2019, 6:47pm UTC](https://community.gravityforms.com/t/validate-code-entry-vs-mysql-db/1893 "2019-05-20T18:47:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![StriveCreative](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@StriveCreative](https://community.gravityforms.com/u/StriveCreative)\
**Post date:** [May 20, 2019, 6:47pm UTC](https://community.gravityforms.com/t/validate-code-entry-vs-mysql-db/1893/1 "2019-05-20T18:47:39Z")

</div>

Hi guys and gals,

Great plugin! Attempting to build a form to help us with a client request. They are running a contest where users will enter a unique code to register for a special event. We want to prevent each code from being used more than one time and codes must match predetermined list of codes. I am attempting to do this via a MySQL call via gform\_validation which checks against already existing codes and whether they have already been submitted. On a multipage form, I would like the code to be validated before the are allowed to move to the second page. However the validation never seems to take place, and the form proceeds on to the second page of form. Here is the code I have written to accomplish this. Could you assist in letting me know where its failing?

```
add_filter( 'gform_field_validation_7_1', 'validate_code' );

function validate_code($validation_result){
	$form = $validation_result['form'];

	foreach( $form['fields'] as &$field ) {

	if ( strpos( $field->cssClass, 'validate-code' ) === false ) {
	    continue;
		}
	}

	$field_value = rgpost( "input_1" );

	$is_valid = is_code( $field_value );

	if ( $is_valid ) {
	   /continue;
	}

	$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){
	// check the DB for the correct USPBL Code and validate. Upon success update row to make sure
	$info = 'mysql response';

	$conn = mysqli_connect("$hostname", "$u", "$p", "$d");
	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';
	    exit();
	}

	//$code = mysqli_real_escape_string($conn, $_POST['email']);

	$sql_check = mysqli_query($conn, "SELECT * FROM $t WHERE codes = '$code' AND valid = 0");
	$sql_checkrows = mysqli_num_rows($sql_check);
	if ($sql_checkrows > 0) {
		$sql_update = "UPDATE $t SET valid = 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;
		}
		mysql_close($conn);
	}
}
```

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [May 21, 2019, 11:28am UTC](https://community.gravityforms.com/t/validate-code-entry-vs-mysql-db/1893/2 "2019-05-21T11:28:01Z")

</div>

I have found that it’s easiest to determine where this is going wrong if you use the built in Gravity Forms logging.

> **[Logging and Debugging](https://docs.gravityforms.com/logging-and-debugging/)**
>
> For quite a while, enabling logging within Gravity Forms has been a go-to method for debugging any issues within Gravity Forms. As of Gravity Forms version 2.2, logging functionality has been moved from a separate add-on to becoming a core feature of...

Enable logging for Gravity Forms core. Then, add some custom logging statements. I like to log any variable right after you set the value, or inside a loop to be sure you made it into the log.

> **[Custom Logging Statements](https://docs.gravityforms.com/custom-logging-statements/#writing-to-the-core-log)**
>
> While Gravity Forms and its add-ons include numerous logging statements sometimes you may need to add more. This article explains how to add custom logging.

Also, why aren’t you using all four arguments for the filter?

```auto
add_filter( 'gform_field_validation_7_1', 'validate_code', 10, 4 );
function validate_code( $result, $value, $form, $field ) {

```

I like to log the $\_POST right inside the function, and the $value and $field as well.

---

<div class="post-metadata">

**Author:** ![richardw8k](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/richardw8k/32/2354_2.png) [@richardw8k](https://community.gravityforms.com/u/richardw8k)\
**Post date:** [January 4, 2022, 5:25pm UTC](https://community.gravityforms.com/t/validate-code-entry-vs-mysql-db/1893/3 "2022-01-04T17:25:30Z")

</div>


