PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 8, 2022

[FIXED] Save data in custom database from contact form 7

 January 08, 2022     contact-form-7, wordpress     No comments   

Issue

I am trying to save data in another database on the same server, but it's not working. The following code I have written in the theme function.php file

function contactform7_before_send_mail( $form_to_DB ) {
    //set your db details
    $mydb = new wpdb('user','password','database','localhost');
    $form_to_DB = WPCF7_Submission::get_instance();
    if ( $form_to_DB )
        $formData = $form_to_DB->get_posted_data();
        $your_email = $form_to_DB->get_posted_data( 'EmailAddress' );
        $emailaddress = implode( ', ', (array) $your_email );
    
        $your_solution = $form_to_DB->get_posted_data( 'solution' );
        $solution = implode( ', ', (array) $your_solution );
    
        $your_FullName = $form_to_DB->get_posted_data( 'FullName' );
        $FullName = implode( ', ', (array) $your_FullName );
    
        $your_PhoneNumber = $form_to_DB->get_posted_data( 'PhoneNumber' );
        $PhoneNumber = implode( ', ', (array) $your_PhoneNumber );
    
        $your_country = $form_to_DB->get_posted_data( '_raw_country' );
        $country = implode( ', ', (array) $your_country );
    
        $your_CompanyName = $form_to_DB->get_posted_data( 'CompanyName' );
        $CompanyName = implode( ', ', (array) $your_CompanyName );
    
        $your_primarybusiness = $form_to_DB->get_posted_data( 'primarybusiness' );
        $primarybusiness = implode( ', ', (array) $your_primarybusiness );
    
        $your_job = $form_to_DB->get_posted_data( 'job' );
        $job = implode( ', ', (array) $your_job );
    
        $your_Info = $form_to_DB->get_posted_data( 'Info' );
        $Info = implode( ', ', (array) $your_Info );
    
    
        $mydb->insert( 'tableName', array( 'fullname' =>$fullname,'emailaddress' =>$emailaddress,'phonenumber' =>$phonenumber,'country' =>$country,'companyname' =>$companyname,'job' =>$job,'primarybusiness' =>$primarybusiness,'solution' =>$solution,'info' =>$info ), array( '%s','%s','%s','%s','%s','%s','%s','%s','%s' ) );
}

add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );

This is the reference link add_action( 'wpcf7_before_send_mail') doesnt call/hook my function

https://contactform7.com/2020/07/28/accessing-user-input-data/


Solution

You can try this function. Your code can be simplified quite a bit. I also sanitized since you're inserting into a DB, you can't be too safe.

I can't exactly test this without knowing your form setup, or the input types. However, it didn't seem like you would need to implode text fields into a comma sep list, unless each input field is a <select> in which case, I'd have to revise my answer.

/**
 * Add form data to another DB
 *
 * @param object $contact_form - The Contact form Object.
 * @param bool   $abort - passed by reference to abort sending.
 * @param object $submission - The submission object.
 * @return void
 */
function contactform7_before_send_mail( $contact_form, $abort, $submission ) {
    // set your db details.
    $mydb = new wpdb( 'user', 'password', 'database', 'localhost' );
    if ( $submission ) {
        $form_data = $submission->get_posted_data();
        $mydb->insert(
            'tableName',
            array(
                'fullname'        => sanitize_text_field( $form_data['FullName'] ),
                'emailaddress'    => sanitize_email( $form_data['EmailAddress'] ),
                'phonenumber'     => sanitize_text_field( $form_data['PhoneNumber'] ),
                'country'         => sanitize_text_field( $form_data['_raw_country'] ),
                'companyname'     => sanitize_text_field( $form_data['CompanyName'] ),
                'job'             => sanitize_text_field( $form_data['job'] ),
                'primarybusiness' => sanitize_text_field( $form_data['primarybusiness'] ),
                'solution'        => sanitize_text_field( $form_data['solution'] ),
                'info'            => sanitize_text_field( $form_data['Info'] ),
            ),
            array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )
        );
    }
}
add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail', 10, 3 );


Answered By - Howard E
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing