简体   繁体   中英

Codeigniter *multiple image upload* save to database issue

I'm probably missing a real simple point here but would one of you Codeigniter gurus' be able to help me with my issue. I have a userform which stores information for a sales page, which holds both text and images saved to a database.

I am able to save one image to the database fine, but multiple i'm struggling. Currently all the images are being transferred to the correct folder but I need them to store in the MySQL table.

Form:

      <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>

Controller:

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

Model (this seems to be the issue??):

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;
      }
 }

In your code you use return $insert; .

return act as exit. So when its reach it will stop the foreach and return value imidiatly to controller.

And you no need to use for() loop inside foreach loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM