Skip Navigation

[Resolved] Can you do a database query in the validation filter?

This support ticket is created 8 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 -
- - - - - - -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

Tagged: 

This topic contains 10 replies, has 3 voices.

Last updated by Adriano 8 years, 4 months ago.

Assisted by: Adriano.

Author
Posts
#350815
Screen Shot 2015-12-01 at 3.08.02 PM.png
Screen Shot 2015-12-01 at 3.07.46 PM.png

Hi.

I am trying to validate a loyalty card through CRED. Please take note of the following:

• I have a custom user field called "membership-number"
• I have a CRED form to update the user fields ID number 1910
• I have a pre-defined list of loyalty cards in my MYSQL database called wp_cards
• The columns of the above table in the database are: ID, membership_number, barcode, card_holder, starter_pack

What I am trying to achieve:

I need to validate the user's membership card against the pre-defined database table to ensure that the card has been physically issued. I need to run a database query to query the data before passing errors.

I have done the database query on a page template to check if it is possible and it runds no problem. However, if I add it into the add_filter('cred_form_validate','my_validation',10,2); filter it does not work (I get a blank screen once submitted). If I take the database query out it works. So my question is, Is it possible to do a database query inside the validation filter? And if not, how would we achieve this?

Here is the code on the page template successfully querying the data:

$user_id = get_current_user_id();
		$key = 'wpcf-membership-number';
		$single = true;
		$my_number = get_user_meta( $user_id, $key, $single );
		
		$mylink = $wpdb->get_row( "SELECT * FROM wp_cards WHERE membership_number = $my_number ", ARRAY_N );
		if ($mylink == NULL)
			{
				echo '<h1>No membership number!</h1>';
			} else {
				echo '<h1>My membership card details are:</h1>';
				echo '<p>ID: ' . $mylink[0] . '<br/>'; // prints "10"
				echo 'Membership Number: ' . $mylink[1] . '<br/>'; // prints "10"
				echo 'Barcode: ' . $mylink[2] . '<br/>'; // prints "10"
				echo 'Card Holder: ' . $mylink[3] . '<br/>'; // prints "10"
				echo 'Starter Pack: ' . $mylink[4] . '</p>'; // prints "10"
		
		if($mylink[3] == 0)
			{
				echo '<p>Good news! <span style="font-size:30px; font-weight:800;">Card not assigned</span></p>';
			} else{
				if($mylink[3] !== 0)
				{
					$user_id = $mylink[3];
					$key = 'first_name';
					$single = true;
					$card_holder = get_user_meta( $user_id, $key, $single );
					echo '<p>Card already assigned to <span style="font-size:30px; font-weight:800;">' . $card_holder . '</span></p>';
				}
			}
		}

Here is the code for the CRED form validation with the broken database query:

add_filter('cred_form_validate','my_validation',10,2);
	function my_validation($field_data, $form_data)
		{	
			list($fields,$errors)=$field_data;
			//print_r($fields);

			if ($form_data['id']==1910)
				{
					
					 $my_number = $fields['wpcf-membership-number']['value'];
					 $mylink = $wpdb->get_row( "SELECT * FROM wp_cards WHERE membership_number = $my_number ", ARRAY_N );
					 if ($mylink == NULL)
						 {
							 $errors['wpcf-membership-number']='Invalid membership number';
						 }
			   }

			return array($fields,$errors);
		}

****I will not be able to provide you with FTP details should the need arise as this is a client based website and we need to ensure the safety of our clients details ****

#350947

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi David,

Thank you for contacting our support forum.

I believe this should be possible since we are essentially running php code.

What we can do is to try something to see if it works with another method of getting the value from the form.

From what I know the form is still submitted in a POST request but the validation hook prevents it from creating a post.

So we can essentially get the card value from the POST request so we should change this line

                     $my_number = $fields['wpcf-membership-number']['value'];

To

                     $my_number = $_POST['wpcf-membership-number'];

Please let me know if this works.

Thanks,
Shane

#351050

Hi Shane.

Thanks for that. I was a little bit confused of when I should be using $Fields and $_POST.

Unfortunately it is still crashing when there is a database query within the filter. I tried another query that works outside of the filter and I got the same results where it crashes. If we can't run a DB query in the filter is there a possible workaround?

#351240

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi David,

It seems that we are not able to do this 🙁

What I can do is to consult with our second tier supporters and get back to you as soon as possible on this.

I would like some confirmation from my team whether this is possible or not.

Thanks,
Shane

#351242

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Escalated

#351308

Hi Shane.

Thanks for the assistance.

#351583

Any feedback?

#351675

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi David,

I havn't gotten any response as yet 🙁

I suspect the reason why is due to the load in the 2nd tier support so it would take some time to get to your ticket.

As soon as the ticket is being handled you will get a response.

Thanks,
Shane

#352123

Hello,

I'm Adriano, Toolset support lead. I have been escalated to your thread.

Your very first reply seems OK, the only missing thing is the $wpdb global:

function my_validation($field_data, $form_data)
global $wpdb;

Please let me know if you are satisfied with my reply and any other questions you may have.

Regards,

Adriano Ferreira

#352828

Thank you for your help

#352892

You are welcome.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.