使用 CodeIgniter PHP 将多输入图像保存到数据库时出现问题

issue while saving multi input images to database using codeigniter php

提问人:ZNO 提问时间:9/24/2023 最后编辑:KIKO SoftwareZNO 更新时间:9/24/2023 访问量:55

问:

我有一个表单,其中输入字段很少,包括图像输入字段,图像输入字段允许多上传选项。现在,当用户单击“添加更多”按钮时,包括图像字段在内的几个字段将被复制,因此我将单个字段保存到一个表中,并使用该ID将多个字段数据保存在第二个表中,我执行了以下代码:

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

这里所有字段都正确保存,除了图像,相应标题/日期字段的图像分别保存在单独的行中,我希望它以逗号分隔值的形式存储在相应的标题/日期字段中,我怎样才能做到这一点,提前致谢

php mysql 镜像 codeigniter codeigniter-3

评论

1赞 user1191247 9/24/2023
您似乎在数组的一个键中有一个错别字。当你创建数组时,你有 ,但在附加时你有 。您可以将“加载和初始化”移到循环之外。传递给 load 方法时,无需调用库 initialize 方法,因为它是在实例化时调用的。将多个图像写入逗号分隔的列表是“次优”的。考虑将图像写入自己的表中。$imagesByHeading[$heading_value])'image''images'$config

答: 暂无答案