Codeigniter *多图像上传*保存到数据库问题

时间:2015-10-14 19:47:11

标签: php mysql codeigniter

我可能在这里错过了一个非常简单的观点,但是你们其中一位Codeigniter大师能够帮助我解决我的问题。我有一个userform存储销售页面的信息,该页面包含保存到数据库的文本和图像。

我可以将一个图像保存到数据库中,但是多个我正在努力。目前所有图像都被转移到正确的文件夹,但我需要它们存储在MySQL表中。

形式:

      <div class="form-group">
        <label class="col-lg-4 control-label">Avonics:</label>
            <div class="col-lg-6">
            <?php
              $avonics6 = array(
              'name'        => 'avonics6',
              'class'          => 'form-control',
              'placeholder'   => 'Avonics',
              'value'   => set_value('avonics6')
            );
             echo form_input($avonics6);
            ?>
         </div>
      </div>
    </fieldset>

    <fieldset class="fieldset_class">
     <legend><strong><font color="black">Aircraft Condition</font></strong></legend>
      <div class="form-group">
        <label class="col-lg-4 control-label">Interior:</label>
            <div class="col-lg-6">
            <?php
              $interior = array(
              'name'        => 'interior',
              'class'          => 'form-control',
              'placeholder'   => 'Interior 0-100%',
              'value'   => set_value('interior')
            );
             echo form_input($interior);
            ?>
         </div>
      </div>
      <div class="form-group">
        <label class="col-lg-4 control-label">Exterior:</label>
            <div class="col-lg-6">
            <?php
              $exterior = array(
              'name'        => 'exterior',
              'class'          => 'form-control',
              'placeholder'   => 'Exterior 0-100%',
              'value'   => set_value('exterior')
            );
             echo form_input($exterior);
            ?>
         </div>
      </div>
</fieldset>

<fieldset class="fieldset_class">
  <legend><strong><font color="black">Upload Images</font></strong></legend>
      <div class="form-group">
        <label class="col-lg-4 control-label">Add Image (1)</label>
            <div class="col-lg-6">
            <?php
             $aircraft1 = array(
             'name'   => 'userfile[]',
             'class'  => 'form-control',
             'id' => 'userfile',
             'type' => 'file',
             'multiple' => ''
           );
            echo form_upload($aircraft1);
          ?>
         </div>
      </div>
       <div class="form-group">
        <label class="col-lg-4 control-label">Add Image (2)</label>
            <div class="col-lg-6">
            <?php
             $aircraft2 = array(
             'name'   => 'userfile[]',
             'class'  => 'form-control',
             'id' => 'userfile',
             'type' => 'file',
             'multiple' => 'true'
           );
            echo form_upload($aircraft2);
          ?>
         </div>
      </div>
      </fieldset>
      <div class="form-group">
        <label class="col-lg-4 control-label"></label>
            <div class="col-lg-8">
            <?php echo form_submit('submit', 'Add Aircraft', 'class="btn btn-danger"');?>
      </div>
    </div>
        <?php
        echo form_close();
        ?>
    </div>
</div><div class="clearfix"></div>

控制器:

function add_aircraft()
{
   $this->load->library('form_validation');
  /* handle form data then send to model */
    $this->form_validation->set_rules('title', 'Title', 'trim|required');
    $this->form_validation->set_rules('price', 'Price', 'trim|required');
    $this->form_validation->set_rules('year', 'Year', 'trim|required');
    $this->form_validation->set_rules('annual', 'Annual', 'trim|required');
    $this->form_validation->set_rules('serial_number', 'Serial number', 'trim|required');
    $this->form_validation->set_rules('airframe_hours', 'Airframe hours', 'trim|required');
    $this->form_validation->set_rules('engine_type', 'Engine type', 'trim|required');
    $this->form_validation->set_rules('engine_hours', 'Engine hours', 'trim|required');
    $this->form_validation->set_rules('propeller_type', 'Propeller type', 'trim|required');
    $this->form_validation->set_rules('notes', 'Sales Pitch', 'trim|required');
    $this->form_validation->set_rules('avonics', 'Avonics', 'trim');
    $this->form_validation->set_rules('avonics1', 'Avonics', 'trim');
    $this->form_validation->set_rules('avonics2', 'Avonics', 'trim');
    $this->form_validation->set_rules('avonics3', 'Avonics', 'trim');
    $this->form_validation->set_rules('avonics4', 'Avonics', 'trim');
    $this->form_validation->set_rules('avonics5', 'Avonics', 'trim');
    $this->form_validation->set_rules('avonics6', 'Avonics', 'trim');
    $this->form_validation->set_rules('interior', 'Interior', 'trim|required');
    $this->form_validation->set_rules('exterior', 'Exterior', 'trim|required');
    $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
    if($this->form_validation->run() == FALSE)
    {
        $data['main_content'] = 'control/aircraftsales/add';
        $this->load->view('control/includes/template_simple_header_footer', $data);
        $name_array[] = $data['aircraft1'];
    }
    else
    {
        $this->load->model('control/Aircraftsales_model');

        if($this->Aircraftsales_model->add_aircraft()){
             $data['main_content'] = 'control/aircraftsales/add';
             $this->load->view('control/includes/template_simple_header_footer', $data);
        }
    }
}

模型(这似乎是问题??):

function add_aircraft()
{
    $name_array = array();
    $count = count($_FILES['userfile']['size']);
    foreach($_FILES as $key=>$value)
        for($s=0; $s<=$count-1; $s++)
        {
          $_FILES['userfile']['name']=$value['name'][$s];
          $_FILES['userfile']['type'] = $value['type'][$s];
          $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
          $_FILES['userfile']['error'] = $value['error'][$s];
          $_FILES['userfile']['size'] = $value['size'][$s];

          /* config image */
          $config['upload_path'] = './upload/aircraftsales';
          $config['allowed_types'] = 'gif|jpg|png';
          $this->load->library('upload', $config);
          $this->upload->initialize($config);

          $this->upload->do_upload();
          $data = $this->upload->data();

          $name_array[] = $data['file_name'];
          $names= implode(',', $name_array);
        }

        $aircraft = array(
            'title' => $this->input->post('title'),
            'price' => $this->input->post('price'),
            'year' => $this->input->post('year'),
            'annual' => $this->input->post('annual'),
            'serial_number' => $this->input->post('serial_number'),
            'airframe_hours' => $this->input->post('airframe_hours'),
            'engine_type' => $this->input->post('engine_type'),
            'engine_hours' => $this->input->post('engine_hours'),
            'propeller_type' => $this->input->post('propeller_type'),
            'notes'     => $this->input->post('notes'),
            'avonics'   => $this->input->post('avonics'),
            'avonics1'  => $this->input->post('avonics1'),
            'avonics2'  => $this->input->post('avonics2'),
            'avonics3'  => $this->input->post('avonics3'),
            'avonics4'  => $this->input->post('avonics4'),
            'avonics5'  => $this->input->post('avonics5'),
            'avonics6'  => $this->input->post('avonics6'),
            'interior'  => $this->input->post('interior'),
            'exterior'  => md5($this->input->post('exterior')),
           );

           $insert = $this->db->insert('aircraftsales', $aircraft);
           return $insert;
      }
 }

1 个答案:

答案 0 :(得分:0)

在您的代码中,您使用return $insert;

return作为退出。因此,当它达到它时,它将停止foreach并将值重新返回给控制器。

您无需在for()循环

中使用foreach循环