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



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

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));
}

此代码没有给出错误,但它不是保存表列中的" hstatus'value" performer_tags'= 5。

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

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 ); 
    }
}

测试代码以下代码对我来说正常。请确保表名和字段名称正确。如果仍然不工作,请调试,让我们知道您找到的确切错误。

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); 
   }
 }

最新更新