以联系表格7获取自动增量ID

时间:2016-06-10 09:46:50

标签: php wordpress

当用户提交表单时,我很难在动态字段中使用自动增量ID。如何为每个提交条目更新自定义表? &安培;我们应该如何从自定义表中获取id?

使用自动增量创建表:

function create_db() {
    global $wpdb;

    $charset_collate = $wpdb->get_charset_collate();
    $table_name = $wpdb->prefix . "id_count";
    if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
        $sql = "CREATE TABLE $table_name (
                `id` mediumint(9) NOT NULL AUTO_INCREMENT,
                `name` mediumtext NOT NULL,
                UNIQUE KEY id (id)
        )$charset_collate;";

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }

}   
add_action("init", "create_db");

表单提交时:

function your_wpcf7_posted_data( $posted_data ) {
    global $wpdb;
    $name = $posted_data['your-name'];

    $table_name = $wpdb->prefix . "id_count";
    $posted_data['cf7-counter'] = $wpdb->insert_id;  
);

2 个答案:

答案 0 :(得分:0)

您的sql代码似乎无法正常运行,因此您可以尝试以下代码:

$sql = "CREATE TABLE IF NOT EXISTS  $table_name (
            `id` int(9) NOT NULL,
             `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,

)ENGINE = MyISAM AUTO_INCREMENT = 262 DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci“;

答案 1 :(得分:0)

嗨如果您正在查看动态数字生成到联系表格7,这个WordPress指南将帮助您。

<强>步骤:

主题function.php添加此代码

function wpcf7_generate_rand_number( $wpcf7_data ) {
$properties = $wpcf7_data->get_properties();
$shortcode = '[rand-generator]';
$mail = $properties['mail']['body'];
$mail_2 = $properties['mail_2']['body'];
if( preg_match( "/{$shortcode}/", $mail ) || preg_match( "/[{$shortcode}]/", $mail_2 ) ) {
$option = 'wpcf7sg_' . $wpcf7_data->id();
$sequence_number = (int)get_option( $option ) + 1;
update_option( $option, $sequence_number );</code>
$properties['mail']['body'] = str_replace( $shortcode, $sequence_number, $mail );
$properties['mail_2']['body'] = str_replace( $shortcode, $sequence_number, $mail_2 );
$wpcf7_data->set_properties( $properties );
}
}
add_action( 'wpcf7_before_send_mail', 'wpcf7_generate_rand_number' );

然后使用字段[rand-generator]

来源: https://www.banna360.com/contact-from7-dynamic-number-generation/