Images not updating properly in Database
-
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)
Viewing 1 replies (of 1 total)
- The topic ‘Images not updating properly in Database’ is closed to new replies.