从wordpress中的自定义用户配置文件字段将数据插入多个表

时间:2018-03-26 11:34:58

标签: php sql database wordpress

我在wp admin中创建了一个自定义配置文件字段,并将其保存到usermeta表。但是我需要将这些数据保存在另一个表'wp_ppv_performer_profile'中。此字段是admin中的下拉字段。 我的代码:

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {
global $wpdb;
    $phs = $_POST['hstatus'];
if ( !current_user_can( 'edit_user', $user_id ) )
    return false;

update_usermeta( $user_id, 'hstatus', $_POST['hstatus'] );


$wpdb->insert( $wpdb->wp_ppv_performer_profile, array("performer_tags" => 
$phs ), array( "performer_id", 5));
}

此代码没有给出错误,但是对于performer_id = 5,表格列'performer_tags'中没有保存'hstatus'值。

2 个答案:

答案 0 :(得分:0)

这是一个工作示例,请确保表名正确无误:

add_action('edit_user_profile_update', 'my_save_extra_profile_fields');
function my_save_extra_profile_fields($user_id) {
    if ( current_user_can('edit_user',$user_id) ){
        global $wpdb;
        $status = $_POST['hstatus'];
        update_user_meta($user_id, 'hstatus', $status);

        // change to performer_profile if ppv_ is part of the prefix, only use the table name without prefix.

        $table = $wpdb->prefix . "ppv_performer_profile";

        // performer_tags and performer_id are assumed to be table columns, status is also assumed to be a string here.

        $data = array( 'performer_tags' => $status, 'performer_id' => 5);
        $format = array( '%s', '%d');
        $wpdb->insert( $table, $data, $format ); 
    }
}

答案 1 :(得分:0)

以下代码经过测试,对我来说工作正常。请确保表名和字段名正确无误。如果仍然无法正常工作,请调试并告诉我们您找到的确切错误。

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
 function my_save_extra_profile_fields($user_id) {
  if ( current_user_can('edit_user',$user_id) ){
    global $wpdb;
    $status = $_POST['hstatus'];
    update_user_meta($user_id, 'hstatus', $status);    

    $table_name= $wpdb->prefix . "ppv_performer_profile";  // Tablename is wp_ppv_performer_profile , I use $wpdb->prefix which will take defined prefix of wordpress (wp_).


    $insertdata= array( 'performer_tags' => $status, 'performer_id' => 5);       
    $wpdb->insert( $table_name, $insertdata); 
   }
 }