Trying to copy Gravity Form entries to Mysql Table in wpdb

I’m trying to copy Gravity Form entries, on each submission a form, to a separate MySql table in the wpdb.

Table CWRoles is already set up and I’m using the following php snippet. Any ideas why it isn’t working please?

Thanks

add_action(“gform_after_submission_7”, “input_fields”, 10, 2);
function input_fields($entry, $form) {
global $wpdb;
$tablename=$wpdb->prefix.“CWRoles”;
$RoleCode = $entry[‘1’];
$RoleTitle = $entry[‘2’];
$RoleDate = date_created;
$Roleid = id;
$Roleuser = user;
$Roleip = ip;
$SQL = “INSERT INTO CWRoles (cwRoleTitleCode,cwRoleTitle,cwrdatecreated,cwrentryid,cwruser,cwrip) VALUES (
‘$RoleCode’,’$RoleTitle’,’$RoleDate’,’$Roleid’,’$Roleuser’,’$Roleip’)”;
$wpdb->query($SQL);
}

From a quick look at your code, none of these variables are defined. You will need to assign the property from the entry to your $RoleDate, $Roleuser, $Roleid and $Roleip variables. Right now, if you have debugging on, you would likely get betting ‘undefined’ warnings. The code will continue, but it won’t do what you want it to.

I recommend adding your own custom logging statements to yourcode, so you can see where the code falls apart. You can find out how to enable logging here: Logging and Debugging - Gravity Forms Documentation

Once you have enabled logging, it’s time to add your own logging statements to your own code, to see if your variables hold the values you expect them to, and your function calls are returning the correct values.

If you have any questions about setting up the custom logging, please let us know.

Chris, Thanks

Understood.
As an experiment I have deleted/blanked those variables from the code so the it reads as below. It worked. Had to hard code the id field as its a primary key in the table, so it is not a final solution but at least its writing to the table. Had to delete the reference to the prefix so that table name is simply “CWRoles”. I do need to find a way to include the blanked out variables as they represent: (1) the date (and time) when the form entry was submitted(created_date), (2) the (id) of the entry form submission ( I guess this is set post submission) , (3) the (user) who submitted the form; and (4) , the user’s (ip). I guess there is a way of grabbing those values as they are stored somewhere? Maybe I have a work around by using the Perk I have installed ‘Populate Anything’ . I suspect I’ll still need a PHP snippet to define the submission id value post submission of the form.

Thanks

add_action(“gform_after_submission_7”, “input_fields”, 10, 2);

function input_fields($entry, $form) {
global $wpdb;
$tablename=“CWRoles”;
$RoleCode = $entry[‘1’];
$RoleTitle = $entry[‘2’];
$SQL = “INSERT INTO $tablename (cwRoleTitleCode,cwRoleTitle,cwrdatecreated,cwrentryid,cwruser,cwrip) VALUES (
‘$RoleCode’,’$RoleTitle’,’’,‘3’,’’,’’)”;
$wpdb->query($SQL);
}

Chris

Subsequently, I have managed to identify the correct fields and set the variables using the following:

$Roleid = rgar( $entry, ‘id’ );
$RoleDate = rgar( $entry, ‘date_created’ );
$Roleip = rgar( $entry, ‘ip’ );
$Roleuser = rgar( $entry, ‘created_by’ );

So that all populates fine. My only issue now is that I am using WpDatatables and their GF Integration Add-on. With this I am able to edit and delete the GF entries. Trouble is that if then edit or delete a previous GF entry it changes the GF entries and the WPDatatable on my site but my code snippet does not handle feeding that delete/edit in the CWRoles table. I’m obviously going to have to add some more PHP to reflect any edits or deletions in the table CWRoles. I’m getting there in steady steps. Hopefully there is a php post submission function that will enable me to pass an edit/deletion.

Therefore, I’ve solved my original problem. Now I have a new one to fix!

Thanks for your initial help/comment. It set me on the right path.

Cheers

Chris

How can I reflect Edits and Deletes through to my separate table? I can edit and delete GF entries from the front end via WPDatatables + Gravity Integration. I understand the modal for the deletion and edit is from the Gravity side? Therefore I am interested to know if there are any hooks that can help me to pass any all edits and deletes to my separate sql table of entry data?

Thanks

Hi Chris. How about the gform_delete_entry hook?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.