从联系表 7 将数据保存在自定义数据库中
Posted
技术标签:
【中文标题】从联系表 7 将数据保存在自定义数据库中【英文标题】:Save data in custom database from contact form 7 【发布时间】:2022-01-20 07:45:25 【问题描述】:我正在尝试将数据保存在同一服务器上的另一个数据库中,但它不起作用。以下代码我写在了主题function.php文件中
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' );
这是参考链接add_action( 'wpcf7_before_send_mail') doesnt call/hook my function
https://contactform7.com/2020/07/28/accessing-user-input-data/
【问题讨论】:
【参考方案1】:你可以试试这个功能。您的代码可以简化很多。由于您要插入数据库,因此我也进行了消毒,您不能太安全。
如果不知道您的表单设置或输入类型,我无法对此进行准确测试。但是,您似乎不需要将文本字段内爆到逗号分隔列表中,除非每个输入字段都是 <select>
在这种情况下,我必须修改我的答案。
/**
* 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 );
【讨论】:
谢谢,这对我有用。唯一的问题是输入字段 我通过这个 $your_job = $form_data['job']; 得到了以上是关于从联系表 7 将数据保存在自定义数据库中的主要内容,如果未能解决你的问题,请参考以下文章
将项目添加到 .plist 并在自定义 UITableViewCell 中显示