issue while saving multi input images to database using codeigniter php

i have a form, where there are few input fields including image input field, the image input fields allow multi upload option. now when a user clicks on add more button few of the fields including image field will get duplicated, so i save the single fields to one table and using that id i save this multiple field data in second table, i did the following code:

if (isset($_POST['addproduct'])) {
    $name = $this->input->post('name');
    $country = $this->input->post('country');
    $city = $this->input->post('city');
    $heading = $this->input->post('heading');
    $dater = $this->input->post('dater');
    $description = $this->input->post('description');

    $blog_info_data = array(
        'name' => $name,
        'country' => $country,
        'city' => $city
    );

    // Insert data into 'blog_info' table and retrieve the inserted ID
    $this->db->insert('blog', $blog_info_data);
    $blog_info_id = $this->db->insert_id();

    $ImageCount = count($_FILES['pimage']['name']);

    // Create an associative array to store image file names grouped by heading
    $imagesByHeading = array();

    for ($i = 0; $i < $ImageCount; $i++) {
        $_FILES['file']['name'] = $_FILES['pimage']['name'][$i];
        $_FILES['file']['type'] = $_FILES['pimage']['type'][$i];
        $_FILES['file']['tmp_name'] = $_FILES['pimage']['tmp_name'][$i];
        $_FILES['file']['error'] = $_FILES['pimage']['error'][$i];
        $_FILES['file']['size'] = $_FILES['pimage']['size'][$i];

        // File upload configuration
        $uploadPath = './uploads/products/';
        $config['upload_path'] = $uploadPath;
        $config['allowed_types'] = 'jpg|jpeg|png|gif';

        // Load and initialize upload library
        $this->load->library('upload', $config);
        $this->upload->initialize($config);

        // Upload file to server
        if ($this->upload->do_upload('file')) {
            // Uploaded file data
            $imageData = $this->upload->data();
            $imageCsv = $imageData['file_name']; // Store the image file name for the current row

            // Get the corresponding details for the current row
            $date_value = isset($dater[$i]) ? $dater[$i] : '';
            $heading_value = isset($heading[$i]) ? $heading[$i] : '';
            $description_value = isset($description[$i]) ? $description[$i] : '';

            // Check if the heading already exists in the associative array
            if (isset($imagesByHeading[$heading_value])) {
                // Append the image file name to the existing heading entry
                $imagesByHeading[$heading_value]['images'][] = $imageCsv;
            } else {
                // Create a new entry for the heading with an array of images
                $imagesByHeading[$heading_value] = array(
                    'dater' => $date_value,
                    'description' => $description_value,
                    'image' => array($imageCsv),
                );
            }
        } else {
            // Handle upload error here (e.g., log the error)
            // You can add error handling code here
        }
    }

    // Iterate through the associative array and insert data into 'blogdetails' table
    foreach ($imagesByHeading as $heading_value => $data) {
        // Concatenate the image file names with commas
        $imagesCsv = implode(',', $data['image']);

        $blog_details_data = array(
            'blogid' => $blog_info_id,
            'dater' => $data['dater'],
            'heading' => $heading_value,
            'image' => $imagesCsv, // Comma-separated image file names
            'description' => $data['description'],
        );

        $this->db->insert('blogdetails', $blog_details_data);
    }

    $this->session->set_flashdata("Successs", "Blog Added Successfully!");
}

here all the fields are getting saved correctly excep the images, the images for the corresponding heading/date field is saved in separate rows individually, i want it to be stored as comma seaparated values in the corresponding heading/date field, how can i accomplish this, thanks in advance