我在wordpress中的网站。我想将联系表格7的值自动发送到其他数据库表。例如,一个是wordpress数据库表,另一个是php数据库表。我该如何解决这个问题?
答案 0 :(得分:2)
遵循以下步骤,并在functions.php中添加代码:
1)在自定义数据库中创建自定义表
CREATE TABLE candidate(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50)
);
2)创建联系表的7个字段
[text* title]
[submit "Send"]
3)将以下代码添加到function.php
function contactform7_before_send_mail( $form_to_DB ) {
//set your db details
global $wpdb;
$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB )
$formData = $form_to_DB->get_posted_data();
$title = $formData['title'];
/************* Insert in wordpress database ***********/
$wpdb->insert( 'candidate', array( 'title' =>$title ), array( '%s' ) );
/************ For Php Mysql Database (Create connection first) ********/
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (title)
VALUES ($title)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );
答案 1 :(得分:0)
将以下代码添加到您的function.php文件中。
function custom_db_wpcf7_before_send_mail($form7_data) {
//set your database details
$custom_db = new wpdb('user','password','custom_db','host');
$form7_data = WPCF7_Submission::get_instance();
if ($form7_data)
$post_data = $form7_data->get_posted_data();
$title = $post_data['title'];
$custom_db->insert('custom_table', array('field' => $value), array( '%s' ));
}
remove_all_filters('wpcf7_before_send_mail');
add_action('wpcf7_before_send_mail', 'custom_db_wpcf7_before_send_mail' );
}