我想使用以下代码在数据库中保存 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.
答案 0 :(得分: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 Plugin会在提交时自动保存您的表单。
答案 1 :(得分: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
) );