Sending checkbox entries to a custom table in wordpress not working

Hi,
Apologies for the question if it is a duplicate, but I have not been able to find a solution.

In wordpress, I am capturing the input from a form and grabbing it and sending it to a custom table like so:

add_action('gform_after_submission_1', 'add_entry_to_db', 10, 2);

function add_entry_to_db($entry, $form) {
  $first_name = $entry[1]; //a normal text input & varchar field
  $topic = $entry[2]; // a checkbox input with 3 options & varchar field

global $wpdb;

// add form data to custom database table
	$wpdb->insert(
	    'zzz_customtable',
	    array(

    'first_name' => $first_name,
    topic' => $topic
	    )
	);
}

I can’t get any input to appear in the database from the $topic variable.
Any ideas where I am going wrong?
I can get inputs from all other field types.

Thanks!
Jason

I don’t have any idea where you are going wrong. But when doing something custom like this, you should log everything. Be sure that logging is active on the site already for Gravity Forms Core (to log all messages.) Then add some logging statements to your code. Follow the examples here:

I recommend logging every variable or object after you think you have set the value, to be sure it contains what you expect it to. Log the entry right after you enter the function. Log the results from $wpbd->insert. Do something like this:

$result = $wpdb->insert(
	'zzz_customtable',
	array(
		'first_name' => $first_name,
		'topic' => $topic
	)
);

GFCommon::log_debug( __METHOD__ . "(): WPDB Insert result is {$result}." );

You might also consider using $wpdb->prepare rather than insert directly.
https://codex.wordpress.org/Function_Reference/wpdb_Class#Protect_Queries_Against_SQL_Injection_Attacks

Hi,
So I figured this out eventually; sorry for not posting sooner.

The issue is accessing the way GF does arrays from checkbox inputs.
I discovered in the documentation that it is done like so:
$topic[β€˜2.3’] // This accesses the third variable in the check box array.

It is important to note that this does not follow the arrays starting at 0 convention; so instead of the third variable being $topic[β€˜2.2’], it is in fact $topic[β€˜2.3’].

Hope this helps someone else!
Kind regards
Jason

1 Like

Thank you for posting that update!