使用codeigniter在mysql中插入多行

时间:2013-03-28 09:04:30

标签: mysql codeigniter

我正在尝试上传多个带有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);
                }

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

3 个答案:

答案 0 :(得分:0)

将更改名称=“description []”更改为name =“description

答案 1 :(得分: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];
    ...
}

试试这个,让我们知道你是怎么过的!

答案 2 :(得分:0)

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