Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Gravity Forms After Submission WordPress

  • SOLVED

Hi, I am trying to send data from my gravity forms to a MySQL database.

This is the code that I am trying to use, I have inserted it into my theme's functions.php but I cannot see it working, it also does not throw an error. I have 5 forms on my site, each share the same fields.

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

$Name = $entry["1"];
$Phone = $entry["4"];
$Email = $entry["7"];
$Website = $entry["5"];
$Message = $entry["3"];

$con=mysqli_connect("localhost","my_user","password","my_database");
mysqli_query($con,"INSERT INTO table (Name, Phone, Email, Website, Message) VALUES ('$Name', '$Phone', '$Email', '$Website', '$Message')");
}


My database TABLE is called 'leads', however I cannot see where to define that?


Some posts on this:
http://stackoverflow.com/questions/17003762/using-gravity-forms-gform-after-submission-to-push-submitted-data-to-mysql
http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/actions/gform_after_submission/

Can anyone help?
Thanks

Answers (4)

2015-02-06

Jeremy answers:

Is this a 3rd party database or your default Wordpress database?

If Wordpress, then try the following:

add_action('gform_after_submission', 'custom_add_entry_to_db', 10, 2);
function custom_add_entry_to_db($entry, $form) {



$Name = $entry["1"];
$Phone = $entry["4"];
$Email = $entry["7"];
$Website = $entry["5"];
$Message = $entry["3"];


global $wpdb;

// add form data to custom database table
$wpdb->insert(
'table',
array(
'Name' => $Name,
'Phone' => $Phone,
'Email' => $Email,
'Website' => $Website,
'Message' => $Message
)
);

}


Ross Gosling comments:

Hi Jeremy, it is not the wp database, but on the same server.

Thanks


Jeremy comments:

I would follow up with Kyle's suggestion and test your connection to the database with something like the following:

$mysqli_connection = new MySQLi("localhost","my_user","password","my_database");
if ($mysqli_connection->connect_error) {
echo "Not connected, error: " . $mysqli_connection->connect_error;
}
else {
echo "Connected.";
}


Jeremy comments:

mysqli_query($con,"INSERT INTO `leads` (Name, Phone, Email, Website, Message) VALUES ('$Name', '$Phone', '$Email', '$Website', '$Message')");


Ross Gosling comments:

Thank you, I have tested the code and get 'Connected'.


Jeremy comments:

If your table is named leads, then your code should be as follows:

add_action("gform_after_submission", "push_fields", 10, 2);

function push_fields($entry, $form){



$Name = $entry["1"];

$Phone = $entry["4"];

$Email = $entry["7"];

$Website = $entry["5"];

$Message = $entry["3"];



$con=mysqli_connect("localhost","my_user","password","my_database");



if (!$con){

die("Connection error: " . mysqli_connect_errno());

}



mysqli_query($con,"INSERT INTO <strong>leads</strong> (Name, Phone, Email, Website, Message) VALUES ('$Name', '$Phone', '$Email', '$Website', '$Message')");



}


Ross Gosling comments:

Thank you, that was it, needed to change table to leads.

2015-02-06

Kyle answers:

Did you check the db connection? Try this to be sure


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

$Name = $entry["1"];
$Phone = $entry["4"];
$Email = $entry["7"];
$Website = $entry["5"];
$Message = $entry["3"];

$con=mysqli_connect("localhost","my_user","password","my_database");

if (!$con){
die("Connection error: " . mysqli_connect_errno());
}

mysqli_query($con,"INSERT INTO table (Name, Phone, Email, Website, Message) VALUES ('$Name', '$Phone', '$Email', '$Website', '$Message')");

}


Ross Gosling comments:

Hi Kyle, I added your code, I cannot see any errors when testing, but fields to not get passed to database.

Thank you

2015-02-06

Ian Lincicome answers:

Maybe try using mysql instead of mysqli so instead of mysqli_connect and mysqli_query, use mysql_connect and mysql_query. A minor problem I had once was solved by doing this, so it's worth a try. Good luck.

2015-02-06

Luis Abarca answers:

You can use wpdb class on the second database.


add_action('gform_after_submission', 'custom_add_entry_to_db', 10, 2);

function custom_add_entry_to_db($entry, $form)
{
$Name = $entry['1'];
$Phone = $entry['4'];
$Email = $entry['7'];
$Website = $entry['5'];
$Message = $entry['3'];

$wpdb2 = new wpdb('your_mysql_user', 'your_mysql_pass', 'dbname', 'dbhost');

// add form data to custom database table
$wpdb2->insert('leads', array(
'Name' => $Name,
'Phone' => $Phone,
'Email' => $Email,
'Website' => $Website,
'Message' => $Message
));
}