How to save field values of Contact Form 7 plugin in database?

I have want to save field value of Contact Form 7 plugin in database using this code:

add_action('wpcf7_before_send_mail', my_conversion($cf7));

function my_conversion($cf7)   
{   
    $name = $cf7->posted_data["your-name"];   
    $email = $cf7->posted_data["your-email"];   
    $Work = $cf7->posted_data["tel-Work"];    
    $homenumber = $cf7->posted_data["homenumber"];   
    $mobilenumber = $cf7->posted_data["mobilenumber"];

    mysql_query("INSERT INTO `hello` (`Your Name`, `Work`, `Home`, `Mobile No`, `Email`) VALUES ('$name',$Work,$homenumber,$mobilenumber,$email)");    
}

but its not working, here’s the error:

"`$cf7->posted_data["...."]`;" can not fetch values.

Related posts

Leave a Reply

2 comments

  1. This is wrong:

    add_action('wpcf7_before_send_mail', my_conversion($cf7));

    It should be:

    add_action('wpcf7_before_send_mail', 'my_conversion');
    

    To know what values are available in your $cf7 object, add var_dump( $cf7 ); at the beginning of your my_conversion function.

    DO NOT USE mysql_query! Use the WPDB Class instead.

    And lastly, there’s the Flamingo Plugin which saves your form automatically upon submission.

  2. yous should try using wp prepared query instead of direct query

    $name = $cf7->posted_data["your-name"];
    $work = $cf7->posted_data["tel-Work"];
    
    $email = $cf7->posted_data["your-email"];
    
    $wpdb->query( $wpdb->prepare( 
        "
            INSERT INTO $wpdb->hello
            ( contact, email , name )
            VALUES ( %d, %s, %s )
        ", 
            $work, 
        $email , 
        $name 
    ) );