2

I am using contact form 7 for creating my form and I have a custom db hosted in the same server that should contain the related data.

I want to store the data from the contact from7 into my custom db and not a wordpress db.

I am doing the below in functions.php now,

add_action('wpcf7_before_send_mail', 'save_form');

function save_form($wpcf7) {

    /* For connecting to database */
    $dbuser = "user";
    $dbpass = "pass";
    $dbhost = "localhost";
    $dbname = "cistom_db";

    // Connect to server and select database.
    $db = mysqli_connect($dbhost, $dbuser, $dbpass) or die("cannot connect");
    mysqli_select_db($db, $dbname) or die("cannot select DB");

    $submission = WPCF7_Submission::get_instance();

    if ($submission) {

        $submited = array();
        $submited['title'] = $wpcf7->title();
    } else {
        echo 'error';
    }

    $insert_query = "insert into candidate(title)values('" . $submited['title'] . "')";

    $result = mysqli_query($db, $insert_query);
    if (!$result) {
        die('Invalid query: $insert_query : ' . mysqli_error($db));
    }
}

However, nothing seems to be working here. Can anyone please help?

manini
  • 358
  • 1
  • 4
  • 14
  • @ Manini as per your requirement solved your issue, Please check my updated code and replay your feedback. – PPL May 08 '18 at 11:30

1 Answers1

8

Follow the steps to store contact form 7 data in custom table:

1) Create Custom table

 CREATE TABLE candidate(
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(50)
);

2) Create contact form 7 fields

[text* title]
[submit "Send"]

3) Add Below code to function.php

  function contactform7_before_send_mail( $form_to_DB ) {
    //set your db details
    $mydb = new wpdb('root','','cistom_db','localhost');

    $form_to_DB = WPCF7_Submission::get_instance();
    if ( $form_to_DB ) 
        $formData = $form_to_DB->get_posted_data();
    $title = $formData['title'];

    $mydb->insert( 'candidate', array( 'title' =>$title ), array( '%s' ) );
}
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );

Hope this works for you.

PPL
  • 6,357
  • 1
  • 11
  • 30
  • thanks for the reply but I already have a custom db which not a part of wp db but is hosted in the same server, so I want to store the data into my custom db – manini May 08 '18 at 11:16
  • Thanks a lot. I was using mysql_query, my bad, – manini May 08 '18 at 11:36
  • Awesome. This is an Amazing solution. but I have a question. Lets's say I have Multiple forms and I want to save values in different database Tables. How can I check which Form is submitted and do some actions based on that? – Waeez Aug 31 '18 at 06:43
  • 2
    Its late but can be helpful for some one. You can add hidden field that identify form and on that basis you can insert record in any db or table. @Waeez – krishnaisdinesh Oct 22 '19 at 10:06
  • Thank, It worked for me. And saved a lot of time. Already upvoted. – Hafiz Ameer Hamza May 22 '21 at 12:11