0

我正在尝试在 codeigniter 中上传多个带有描述的图像。一切都很好,图像已上传,图像文件名已发送到数据库但是“描述”不会进入数据库,我在哪里弄错了?

form code is like this 
  File 1: <input type="file" name="image1" /><br />
  Descr: <input type="text" name="description[]" size="50" value="" /><br/><br/>

  File 2: <input type="file" name="image2" /><br />
  Descr: <input type="text" name="description[]" size="50" value="" /><br/><br/>

  File 3:<input type="file" name="image3" /><br />
  Descr: <input type="text" name="description[]" size="50" value="" /><br/><br/>

模型是这个

for($i = 1; $i < 6; $i++) {
                    /* Handle the file upload */
                    $upload = $this->upload->do_upload('image'.$i);
                    /* File failed to upload - continue */
                    if($upload === FALSE) continue;
                    /* Get the data about the file */
                    $data = $this->upload->data();

                    $uploadedFiles[$i] = $data;
                    /* If the file is an image - create a thumbnail */
                    if($data['is_image'] == 1) {
                        $configThumb['source_image'] = $data['full_path'];
                        $this->image_lib->initialize($configThumb);
                        $this->image_lib->resize();
                    }


                        $image['raw'] = $data['raw_name'];
                        $image['ext'] = $data['file_ext'];
                        $image['newsid'] = $_POST['newsid'];
                        $image['description'] = $_POST['description'][$i];
                        $this->db->insert('multipics', $image);
                }

请注意,上面的代码插入了多行,我只需要描述字段进入数据库

4

3 回答 3

0

将更改名称="description[]" 更改为 name="description

于 2013-03-28T09:09:01.377 回答
0

作为一种解决方法,我会首先在循环之外获取 POST 变量:

// (1) Get the POST description variable outside of the look     
// As a precaution, dump or echo $description to make sure it looks right...

$description_ = $this->input->post('description');


for ($i = 1; $i < 6; $i++)
{
    ...
    // Put some checks to make sure $i is an index...
     $image['description'] = $description[$i];
    ...
}

试试这个,让我们知道你的进展情况!

于 2013-03-28T11:18:58.573 回答
0

您可能还想查看 CI 的insert_batch()方法。这将允许您通过一个查询插入所有记录。

于 2013-03-28T14:24:21.733 回答