Using gform_after_submission for pushing entries into MySQL Database

I want to push gravity form entries to a MySQL database. However, I have not managed to connect to this database yet.

I found in this forum the following link: Using gform_after_submission for pushing entries into SQL Database in wordpress with a code that according to the poster worked for him. However, as I said, it is not working for me.

This is the code:

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

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

// Below here I have also tried with: $conn = new mysqli("", "root", "***", "***");

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

I have also tried adding at the end these two lines:


What am I missing? Maybe something out of the code? I have also tried installing two versions of PHP on my computer (8.1 and 7.4)

Hey there, are you using it for all gravity forms or just one specific one?

I use this code for each form so I adjust the gform_after_submission to include the form number, for example, gform_after_submission_5 by adding the _5 it runs for gravity form no 5. I’ve never tried it without the form number.

It also needs to be added to your themes functions.php file

Other than that the code looks fine, I will suggest double-check the above and then how the SQL database is set up as well in my experience the slightest issue with text type and it will miss data.

Hope that helps

Hi Dan,

Thanks for your answer. Without “_5” it should work for all the forms. Anyway, I have tried on both ways. The code is too in my active themes functions.php file. Indeed a simple typo can screw the whole code. In fact, in this link, someone says that he had a problem with a space, but unfortunately he does not say where. I have already checked the code several times. So, I am afraid it must be an issue with how the database is set up. Could you please provide me with more insights here? How should this be set up so I can successfully make the connection work?

Thanks in advance!

So the code looks ok to me, try using the code with one text field in a form and Table.

With your MySQL check for the following

1, date format - MySQL only allows date in the yyyy-mm-dd format nothing else, so bear that in mind if MySQL is set for date.

2, Allow null entries, in your form your submitting fields 7,8 & 9 but if there is more or a field does not contain data make sure MYSQL it may fail.

3, Change function name from input_fields to something less generic it may clash but as I have found out the hard way if your PHP is wrong it can bring the whole site down.

4, doublecheck privileges for your username to be able to add entries into tables

A code I use that works is below

add_action(“gform_after_submission_6”, “go_assets”, 10, 2);
function go_assets($entry, $form){

$Supply_Date = $entry[“1”];
$Reg = $entry[“2”];
$Model = $entry[“3”];


mysqli_query($con,“INSERT INTO TABLENAME (Supply_Date,Reg,Model) VALUES (‘$Supply_Date’,‘$Reg’,‘$Model’)”);

Note the following

“go_assests” in both locations can be updated to your name, both must match
Server IP Address needs changing
Username for database access
Password for database access
Database Name updates to your database name (not table)
TABLENAME is the name of your table.

Hope this helps you through

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