• I have created dynamic add remove file field to insert image in database. But somehow it is not working properly. Following is my code…

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

    Every thing works well but when I select a new image in first field and submit the value to the database, it removes all other images of other fields from database…

    And if I select image for other fields and submit the value to database it then creates new field with that value…

    In simple terms, When I want to update any particular file field in the dynamic add/remove file field then that particular field should only be get updated and other fields should remain intact.

    Pl Help…

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    This is because you are updating using only the new or selected image and not considering all the other elements in the list. If you print_r() the variables used in your code at various points, you can see that the other elements are not used where they are needed.

    You should be working with an array of data, each element being a file reference. Either add a new file reference to the end of this array, or locate a specific element for replacement. Then update using the entire array every time. You cannot update individual elements by themselves.

Viewing 1 replies (of 1 total)
  • The topic ‘Images not updating properly in Database’ is closed to new replies.