skip to Main Content

I have been trying to insert some fields from a gravity forms submission into a custom WP database and can’t seem to get the fields to populate.

// create a past speaking event
add_action( 'gform_after_submission_8', 'add_past_speaking_event', 10, 2 );
function add_past_speaking_event($entry, $form) {
global $wpdb;

  $author = $entry[1];
    $postID = $entry[9];
    $eventTitle = $entry[6];
  $event_description = $entry[8];


  $tablename ="wp_past_speaking_events";

$wpdb->insert($tableName, array('event_id'=> NULL, 'post_id' => $postID, 'author' => $author, 'event_title' => $eventTitle, 'event_description' => $event_description), array('%d', '%d', '%s', '%s', '%s'));
}

I have echoed the $author, $postID etc. and they are all populated with the right data. And I get a success message after form submission. But when I check the DB I don’t see any new entries.

This code is placed in the functions.php file of my child theme.

Any help appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    Ok so I solved this. I never found out why wpdb->insert didn't work but went with wpdb->query instead.

    <?php
    
    // create a past speaking event
    add_action( 'gform_after_submission_8', 'add_past_speaking_event', 10, 2 );
    function add_past_speaking_event($entry, $form) {
    
        global $wpdb;
    
        $author = $entry[1];
        $postID = $entry[9];
        $eventTitle = $entry[6];
        $event_description = $entry[8];
    
        $wpdb->query("INSERT INTO `wp_past_speaking_events` (`event_id`, `post_id`, `author`, `event_title`, `event_description`) VALUES ('NULL', '$postID', '$author', '$eventTitle', '$event_description')");
    
    }
    
    ?>
    

    I ended up just putting the table name into the query as well and getting rid of the $tablename.

    Two things that helped a bunch were

    $wpdb->print_error();
    $wpdb->show_errors();
    

    So now when I submit a gravity form with an ID of 8 the function will run and store the fields in a custom table. If you wanted to do this for every form you would use 'gform_after_submission' instead.


  2. There’s a problem with WordPress versions older than 4.4 as mentioned here #15158 and WordPress doesn’t support null values in wpdb->insert on numeric columns

    So with if you’re using any versions of WordPress below 4.4, you can try and remove event_id from your insert array and see if it works or not

    // create a past speaking event
    add_action( 'gform_after_submission_8', 'add_past_speaking_event', 10, 2 );
    function add_past_speaking_event($entry, $form) {
    global $wpdb;
    
      $author = $entry[1];
        $postID = $entry[9];
        $eventTitle = $entry[6];
      $event_description = $entry[8];
    
    
      $tablename ="wp_past_speaking_events";
    
    $wpdb->insert($tableName, array('post_id' => $postID, 'author' => $author, 'event_title' => $eventTitle, 'event_description' => $event_description), array('%d', '%d', '%s', '%s', '%s'));
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search