Using gform_after_submission for pushing entries into SQL Database in wordpress

Hi All, I’m looking to use gform_after_submission to push data to Custom SQL database within wordpress.

I/m new to php and SQL so I’m reading/copying and learning all in one. I found the below code.

add_action("gform_after_submission_43", "input_fields", 10, 2);
function input_fields($entry, $form){
    global $gravitydb;

$entry["4"] = $_POST["name"];

mysqli_query($con,"INSERT INTO test (name) VALUES ('$name')");

I’ve tried to start basic take small steps

I’ve created Form ID 43 with just name as single line text (its ID is 4)
I’ve created an SQL Database within wordpress named ‘Test’ which has line set as primary key and autoincrement and name set as text.
The code above is placed in my functions.php of my active theme

When I use the form and submit an entry the SQL Database triggers a new Line Entry but no value in name comes through.

My reason to try this is for the users benefit on the front end, I am using GF with beta conditional logic and wpDataTables.

We need full editing through wpDataTables from the form submissions in standard and excel view which allows bulk entry on the front end of the database and by using the gform_after_submission gives us and at the same time giving the user the form for single entry retains the notifications for single entries.

Any help would be great

Thanks in Advance

Hi Dan. Looks like you jumped right in to the learning, reading and copying all at once for sure. I don’t know exactly which part of your code is creating a problem, but I can see some basic errors in the code. You’ll need to go back and learn some more. Here are a few things that don’t make sense:

  1. $gravitydb is not being used anywhere. Why define it as a global?
  2. You are trying to set $entry['4'] to some value from the $_POST, but you’re not actually using the $entry anywhere. You can’t save any changes to an entry in the gform_after_submission hook like that, so that does not make sense.
  3. You are trying to insert $name but $name has no value in this code.

Using gform_after_submission to insert data into another table using SQL is perfectly fine, but your code needs some work. I recommend taking a look at the code, and trying to straighten out why you have defined certain variables the way you do, and how to ensure all the variables have the values you expect them to.

Post again with your next revision. Keep going! Thank you.

HI Chris, well after a couple of days of digging I have managed to learn more :wink: and pieced together the following

add_action("gform_after_submission_43", "input_fields", 10, 2);
function input_fields($entry, $form){

$Name = $entry["7"];
$Date = $entry["8"];
$Email = $entry["9"];

mysqli_query($con,"INSERT INTO test (Name,Date,Email) VALUES ('$Name','$Date','$Email')");

This code seems to work, whether or not it could be improved is a different question but for now, it functions to send data I need to external databases which for me works wonders as it gives my users the functionality of gravity forms with dynamic population and notifications and at the same time my office staff complete editablility.

Thanks for the pointers

I’m glad you figured that out. So long as it works, I think that should be OK.