Gravity form sometimes doesn't write to database.

I have the following code in my functions.php that deals with the processing of a Gravity form input.

[php]
//Study registration post data to custom tables
add_action(‘gform_after_submission_3’, ‘post_to_third_party’, 10, 2);
function post_to_third_party($entry, $form) {
global $wpdb;
$member = sanitize_user($entry[“1”]);
$study = sanitize_text_field(strtolower($entry[“2”]));
$website = esc_url($entry[“3”]);
$abt = sanitize_text_field($entry[“16”]);
$dna = sanitize_text_field($entry[“17”]);
$worldwide = sanitize_text_field($entry[“4”]);
if ($worldwide==“yes”){
$country = “Worldwide”;
}
else {
$country = sanitize_text_field($entry[“5”]);
}
$SQL = “INSERT INTO ts14_studies( study_name, mem_no, worldwide, country, website, abt, dna) VALUES ( ‘$study’ , ‘$member’, ‘$worldwide’, ‘$country’, ‘$website’, ‘$abt’, ‘$dna’)”;
$wpdb->query($SQL);
//Now get ID of the study entry
$study_id = $wpdb->get_col(“select ID from ts14_studies where study_name =’$study’ ORDER BY ID DESC LIMIT 1”);
// Now write to the variants table
// First write master surname
$variant = $study;
$SQL = “INSERT INTO ts14_variants( variant_name, study_id) VALUES ( ‘$variant’, ‘$study_id[0]’ )”;
$wpdb->query($SQL);

		if ($entry["6"] == "Yes"){
		//check fields 7-15 for variants and write to database
		$x = 7;
		while ($x<=15){
		$variant = sanitize_text_field(strtolower($entry[$x]));
		if ($variant == ""){
			break;}
			else {
		$SQL = "INSERT INTO ts14_variants( variant_name, study_id) VALUES ( '$variant', '$study_id[0]' )";
		$wpdb->query($SQL);
		$x++;
		}
		}
		}

}
[/php]

This works fine most of the time. Occasionally this code is not executed

[php]$SQL = “INSERT INTO ts14_studies( study_name, mem_no, worldwide, country, website, abt, dna) VALUES ( ‘$study’ , ‘$member’, ‘$worldwide’, ‘$country’, ‘$website’, ‘$abt’, ‘$dna’)”;
$wpdb->query($SQL);[/php]

Has anyone any ideas why this may be the case, I suspect there is a bug somewhere but I can’t spot it.

Well, first, you have a simple database INSERT query. You do not show any error checking for your queries.
Depending on how your “$wpdb” system is set up, it should have a way of tracking errors that you can
access. Here is an example from one of my working site codes handling users…
[php]
$query = “INSERT users ( userID, user_status, last_date_accessed) VALUES ($user_id, ‘NEW-USER’, $timestamp)”;
$result = mysqli_query($db_connect, $query) or die(“INSERT error - on user log in page: Error inserting new user, error value=” . mysqli_error($db_connect));
[/php]
As you see this is MySQLi and adds in error code to show the error if it fails. I do not see any error code in
your samples. I assume you can pull out the last error from your query using some interface such as this
type of command… " echo $wpdb->error(); " But, this would depend on your $wpdb layout and you would
need to review that code to figure out how to display the error message.

Now, to look into this, I would first look at the database and see if any of the inserted data is limited to a
set format. You may be sending an invalid record to the query interface. For example, if your database has
the “study_name” as an integer value and is a primary key, then you can not enter the same value a
second time. All queries log an error somewhere on the server. Therefore, it would be good to either make
your code display errors if they appear or log into your server’s control panel and review the MySQL error
log to view what the error was.

It is hard to debug this type of error. An occasional error or failure to insert in a DB can be tricky to locate.
Most often, it is something simple that was not validated correctly or simply not thought about. Such as
duplicate entries or badly formatted data.

Not sure if all this helps or not. Let us know if you solve it or need further help…

Thanks for your thoughts on this.

I have decided to do this with a CPT rather than two custom tables as I couldn’t resolve the problem.

Well, glad you solved it. We will mark this one solved. At least it is working for you now!

Sponsor our Newsletter | Privacy Policy | Terms of Service