简体   繁体   English

如何将联系人表单 7 中的用户输入保存到我的 Wordpress 数据库中的自定义表中?

[英]How can I save user input from a Contact Form 7 into a Custom Table in my Wordpress database?

I have a contact form called Check In that I want to store in the table wpxo_checkin_forms .我有一个名为Check In的联系表单,我想将其存储在表wpxo_checkin_forms中。 The columns are: userid , firstname , lastname , weight , checkindate , energylevel (3 radio button options), missmeal (two radio button options), and missworkout (two radio button options).这些列是: useridfirstnamelastnameweightcheckindateenergylevel (3 个单选按钮选项)、 missmeal (两个单选按钮选项)和missworkout (两个单选按钮选项)。

I found this code here that I tried to modify and put in my child theme's function.php, but when I submit the form, it does not save in the wpxo_checkin_forms table.我在这里找到了这段代码,我试图修改并放入我的子主题的 function.php,但是当我提交表单时,它没有保存在wpxo_checkin_forms表中。

What am I doing wrong here?我在这里做错了什么?

add_action( 'wpcf7_submit', 'SE_379325_forward_cf7', 10, 2 );
function SE_379325_forward_cf7( $form, $result ) {
    if ( ! class_exists( 'WPCF7_Submission' ) ) {
        return;
    }
    $submission = WPCF7_Submission::get_instance();
    if ( 'mail_sent' === $result['status'] ) { // proceed only if email has been sent.
        $posted_data = $submission->get_posted_data();
        save_posted_data( $posted_data, $form->id );
    }
}
// your insert function.
function save_posted_data( $posted_data, $form_id ) {
    if ( 1522 !== $form_id ) {
        global $wpdb;
        $wpdb->insert(
            $wpdb->prefix . 'wpxo_checkin_forms',
            array(
                'userid' => $posted_data['userid'],
                'firstname' => $posted_data['first-name'],
                'lastname' => $posted_data['last-name'],
                'checkindate' => $posted_data['checkin-date'],
                'weight' => $posted_data['weight'],
                'energylevel' => $posted_data['energy-level'],
                'missmeal' => $posted_data['miss-meal'],
                'missworkout' => $posted_data['miss-workout']
            ),
            array( '%s' )
        );
    }
}```

Did you debug this code?你调试过这段代码吗? First make sure that your function runs, If it is running then check the email condition.首先确保您的 function 运行,如果它正在运行,则检查 email 条件。 you have to stop emailing for inserting testing.您必须停止发送电子邮件以插入测试。

The following code should work:以下代码应该可以工作:

add_action( 'wpcf7_submit', 'SE_379325_forward_cf7', 10, 2 );
function SE_379325_forward_cf7( $form, $result ) {
    if ( ! class_exists( 'WPCF7_Submission' ) ) {
        return;
    }
    $submission = WPCF7_Submission::get_instance();
    if ( 'mail_sent' === $result['status'] ) { // proceed only if email has been sent.
        $posted_data = $submission->get_posted_data();
        save_posted_data( $posted_data, $form->id );
    }
}
// your insert function.
function save_posted_data( $posted_data, $form_id ) {
    if ( 1522 !== $form_id ) {
        global $wpdb;
        $wpdb->insert(
            $wpdb->prefix . 'wpxo_checkin_forms',
            array(
                'userid' => $posted_data['userid'],
                'firstname' => $posted_data['first-name'],
                'lastname' => $posted_data['last-name'],
                'checkindate' => $posted_data['checkin-date'],
                'weight' => $posted_data['weight'],
                'energylevel' => $posted_data['energy-level'],
                'missmeal' => $posted_data['miss-meal'],
                'missworkout' => $posted_data['miss-workout']
            )
        );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将用户输入与自定义WordPress数据库表进行比较 - How to compare User Input to Custom WordPress Database Table WordPress Contact Form 7插件-如何将输入数据保存到jquery cookie中? - WordPress Contact Form 7 Plugin - How to save input data into jquery cookie? 如何将数据从HTML表单保存到WordPress中的数据库表? - How to save data from an HTML form to a database table in WordPress? 如何将表单输入保存到数据库,将值发送到控制器时遇到问题 - How can I save form input to a database, I'm having trouble sending the values to my controller 如何在联系表7(wordpress)发送电子邮件并随后返回正常流程之前进行过滤? - how can I filter before contact form 7 (wordpress) send email and later go back to my normal flow? 如何将表单数据保存到数据库中 - How can I save my form data into the database 自定义wordpress联系表格 - Custom wordpress contact form 将联系人表单 7“选择字段”中的数据保存在自定义数据库中,而不是保存在 wordpress 数据库中 - Saving data from a contact form 7 "selection field" in a custom database and not in a wordpress database 如何在Wordpress中向用户添加自定义联系人字段 - How to add custom contact fields to the user in wordpress Wordpress-将联系表单7字段另存为数据库 - Wordpress - Save Contact Form 7 fields to the database as separate columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM