将联系表单7数据保存到自定义数据库而不是Wordpress数据库中 [英] Saving contact form 7 data into custom db and not wordpress db

查看:126
本文介绍了将联系表单7数据保存到自定义数据库而不是Wordpress数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用联系表单7创建表单,并且在同一服务器上托管了一个自定义数据库,该数据库应包含相关数据。

I am using contact form 7 for creating my form and I have a custom db hosted in the same server that should contain the related data.

我想存储来自联系人的数据从7进入我的自定义数据库,而不是wordpress数据库。

I want to store the data from the contact from7 into my custom db and not a wordpress db.

我现在在functions.php中执行以下操作,

I am doing the below in functions.php now,

add_action('wpcf7_before_send_mail', 'save_form');

function save_form($wpcf7) {

    /* For connecting to database */
    $dbuser = "user";
    $dbpass = "pass";
    $dbhost = "localhost";
    $dbname = "cistom_db";

    // Connect to server and select database.
    $db = mysqli_connect($dbhost, $dbuser, $dbpass) or die("cannot connect");
    mysqli_select_db($db, $dbname) or die("cannot select DB");

    $submission = WPCF7_Submission::get_instance();

    if ($submission) {

        $submited = array();
        $submited['title'] = $wpcf7->title();
    } else {
        echo 'error';
    }

    $insert_query = "insert into candidate(title)values('" . $submited['title'] . "')";

    $result = mysqli_query($db, $insert_query);
    if (!$result) {
        die('Invalid query: $insert_query : ' . mysqli_error($db));
    }
}

但是,这里似乎没有任何操作。有人可以帮忙吗?

However, nothing seems to be working here. Can anyone please help?

推荐答案

请按照以下步骤在自定义表中存储联系表单7数据:

Follow the steps to store contact form 7 data in custom table:

1)创建自定义表

 CREATE TABLE candidate(
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(50)
);

2)创建联系表7个字段

2) Create contact form 7 fields

[text* title]
[submit "Send"]

3)将以下代码添加到function.php

3) Add Below code to function.php

  function contactform7_before_send_mail( $form_to_DB ) {
    //set your db details
    $mydb = new wpdb('root','','cistom_db','localhost');

    $form_to_DB = WPCF7_Submission::get_instance();
    if ( $form_to_DB ) 
        $formData = $form_to_DB->get_posted_data();
    $title = $formData['title'];

    $mydb->insert( 'candidate', array( 'title' =>$title ), array( '%s' ) );
}
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );

希望这对您有用。

这篇关于将联系表单7数据保存到自定义数据库而不是Wordpress数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆