Saving wordpress contact form 7 submissions on the database

I’m trying to save Contact Form 7 Submissions on the database.
At the moment I’m using CFDB plugin to save them on the database but it saves in multiple rows.

The plugin creates multiple rows for each entry. I’d like to make Contact Form 7 (without the external plugin to save infos on database) to make one row per contact form with the columns being the field names of the contact form.

Read More

Right now I’m using a pivot SQL to display them as I want but I need to save directly them as I’d like (I contacted plugin’s author to ask some help, I’m waiting for a reply)

SELECT
DATE_FORMAT(FROM_UNIXTIME(submit_time), '%b %e, %Y  %l:%i %p') AS Submitted,
MAX(IF(field_name='Name', field_value, NULL )) AS 'Name',
MAX(IF(field_name='CardNum', field_value, NULL )) AS 'CardNum',
MAX(IF(field_name='Exp', field_value, NULL )) AS 'Exp',
MAX(IF(field_name='Amount', field_value, NULL )) AS 'Amount'
FROM wp_cf7dbplugin_submits
WHERE
form_name = 'Purchases'
GROUP BY submit_time
ORDER BY submit_time DESC

So I searched on Google and found a tut.

First I created a table to store the data in the database:

CREATE TABLE `wp_tps_forms` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `form` varchar(100) NOT NULL DEFAULT '',
  `data` text NOT NULL,
  `date` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

and then I added an action on my current functions.php file:

add_action('wpcf7_before_send_mail', 'save_form' );

function save_form( $wpcf7 ) {
   global $wpdb;

   /*
    Note: since version 3.9 Contact Form 7 has removed $wpcf7->posted_data
    and now we use an API to get the posted data.
   */

   $submission = WPCF7_Submission::get_instance();

   if ( $submission ) {

       $submited = array();
       $submited['title'] = $wpcf7->title();
       $submited['posted_data'] = $submission->get_posted_data();

    }

     $data = array(
        'name'  => $submited['posted_data']['name'],
        'email' => $submited['posted_data']['email']
         );

     $wpdb->insert( $wpdb->prefix . 'tps_forms', 
            array( 
                          'form'  => $submited['title'], 
               'data' => serialize( $data ),
               'date' => date('Y-m-d H:i:s')
            )
        );
}

But this code insert all the form’s data inside a single column, how can I for example divide variables in colums? (eg. name, surname, address, etc.)

THANKS IN ADVANCE TO EVERYBODY!!!

Related posts

1 comment

  1. This is an old item now that helped me to solve my problem of integrating data coming from contact form 7 without using CFDB.

    I don’t know if that’s what you want to do but you can add fields to your table and just add variables exploded, like that:

    $wpdb->insert( $wpdb->prefix . 'tps_forms', 
        array( 
            'form'  => $submited['title'], 
            'name' => $data['name'],
            'email' => $data['email'],
            'message' => $data['message'],
            'date' => date('Y-m-d H:i:s')
        )
    );
    

    Thanks!

Comments are closed.