2

我想使用以下代码将Contact Form 7插件的字段值保存在数据库中:

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)");    
}

但它不起作用,这是错误:

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

2 回答 2

2

这是错误的:

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

它应该是:

add_action('wpcf7_before_send_mail', 'my_conversion');

要了解对象中可用的值,请在函数的开头$cf7添加。var_dump( $cf7 );my_conversion

不要使用mysql_query!改为使用WPDB Class

最后,还有Flamingo 插件,它会在提交时自动保存您的表单。

于 2013-05-28T12:10:20.060 回答
0

您应该尝试使用 wp 准备好的查询而不是直接查询

$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 
) );
于 2013-05-28T12:06:46.770 回答