图片在数据库中无法正确更新

时间:2018-07-09 09:11:38

标签: php wordpress foreach

我创建了动态添加/删除文件字段以在数据库中插入图像。但是不知何故它无法正常工作。以下是我的代码...

$member_image_names = array_map( 'sanitize_file_name', $_FILES['member_image']['name'] );
foreach ( $member_image_names as $member_image_name ) {

    if ( empty( $member_image_name ) ) {

        global $wpdb;
        $project_id = $_SESSION['project_id'];
        $project_member_details = $wpdb->get_var( $wpdb->prepare( "SELECT project_members FROM wpxa_orocox_project_members WHERE project_id = %d", $project_id ) );
        $project_member_detail = json_decode( $project_member_details, true );
        $member_image_newname = $project_member_detail['member_image'];

    } else {

        $member_image_ext = strtolower( end( explode( '.', $member_image_name ) ) );
        $member_image_newname[] = get_current_user_id() . $_SESSION['project_id'] . "_" . time() . "_" . mt_rand() . "." . $member_image_ext;

    }

$member_details->member_image = $member_image_newname;

$member_details_encode = wp_json_encode( $member_details );

global $wpdb;

$members_result_update = $wpdb->update( 'wpxa_project_members',

  array( 'project_members' => $member_details_encode ),

  array( 'project_id' => $_SESSION['project_id'] ),

  array( '%s' ),

  array( '%d' )

);

if ( ! empty( $members_result_update ) ) {

$member_details_decode = json_decode($member_details_encode, true);
$count_member_decode = count( $member_details_decode['member_image'] );

for ( $i = 0; $i < $count_member_decode; $i++ ) {
 $m_image_name = $member_details_decode['member_image'][$i]; 

$profile_image_folder = "profile-images/";
$profile_image_path = trim( $profile_image_folder . basename( $m_image_name ) );
$profile_image_temp = $_FILES['member_image']['tmp_name'][$i];
$profile_image_ext = strtolower( end( explode( '.', $m_image_name ) ) );

if ( $profile_image_ext == "jpg" || $profile_image_ext == "png" || $profile_image_ext == "jpeg" || $profile_image_ext == "gif" ) {

move_uploaded_file( $profile_image_temp, $profile_image_path );

}

}

}

}

一切正常,但是当我在第一个字段中选择一个新图像并将值提交到数据库时,它将从数据库中删除其他字段的所有其他图像...

如果我为其他字段选择图像并将值提交到数据库,则它将使用该值创建新字段...

简单来说,当我要更新动态添加/删除文件字段中的任何特定文件字段时,则仅应更新该特定字段而其他字段应保持不变。

Pl帮助...

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题,我做到了:(示例)

if ( empty( $options['index1_cardimg'] ) ) {$options['index1_cardimg'] = get_myoption_value('index1_cardimg');}

如果没有此项检查(如果(空){}),则所有其他字段将被重置。

您只需要用最后的数据填充它们(如果它们不发送)。

具有如下代码:

if ( empty( $members_result_update ) ) {
// fill Other options with last value 
}