2015-11-24 226 views
0

我想進行表單編輯。在編輯表單中,我將製作上傳照片。但是當點擊提交按鈕時,會出現錯誤:您沒有選擇要上傳的文件。有什麼問題? 控制器:無法上傳文件(未選擇要上傳的文件)

public function edit_data($id_artikel='',$foto=''){ 
    $artikel = $this->mymodel->getedit($id_artikel); 
    $data = array(
     "id_artikel"  => $artikel[0]['id_artikel'], 
     "judul"    => $artikel[0]['judul'], 
     "kategori_terpilih" => $artikel[0]['nama_kategori'], 
     "isi"    => $artikel[0]['isi'], 
     "foto"    => $artikel[0]['foto'] 
    ); 
    $kategori = $this->mymodel->get_kategori(); 
    $a['kategori'] = $kategori->result(); 
    $this->load->view('form_edit',$data); 
    } 

public function do_update(){ 
     $config['upload_path'] = 'images/uploaded/'; 
     $config['allowed_types'] ='gif|jpg|png'; 

     $this->load->library('upload',$config); 

     if(!$this->upload->do_upload()){ 
      $sub_data['error']=$this->upload->display_errors(); 
      echo '<pre>'; 
      print_r($sub_data); 
      echo '</pre>'; 

      die;  
     }else{ 
      $result_upload=$this->upload->data(); 
     } 

    $id_artikel  = $_POST['id_artikel']; 
    $judul   = $_POST['judul']; 
    $nama_kategori = $_POST['nama_kategori']; 
    $isi   = $_POST['isi']; 
    $foto   = $result_upload['file_name']; 
    $data_insert = array(
    'id_artikel' => $id_artikel, 
    'judul'   => $judul, 
    'nama_kategori' => $nama_kategori, 
    'isi'   => $isi, 
    'foto'   => $foto); 
    $where = array('id_artikel' => $id_artikel); 
    $res = $this->mymodel->UpdateData('artikel',$data_update,$where); 
    if($res>=1){ 
     $this->session->set_flashdata('pesan','Update Data Sukses'); 
     redirect('crud/index'); 
    } 
    } 

型號:

public function getedit($id_artikel=''){ 
    $data = $this->db->query('SELECT a.id_artikel, a.judul, a.tanggal_update,a.isi, a.foto, b.nama_kategori FROM artikel as a LEFT JOIN kategori as b on a.id_kategori=b.id_kategori where id_artikel = ' .$id_artikel); 
    return $data->result_array(); 
} 
public function UpdateData($tabelNama,$data,$where){ 
    $res = $this->db->update($tabelNama,$data,$where); 
    return $res; 
    } 

觀點:

<html> 
<head> 
    <title>Data Mahasiswa</title> 
</head> 
<body> 
    <form method="POST" action="<?php echo base_url()."index.php/crud/do_update"; ?>"> 
    <table> 
     <tr> 
      <td>Id Artikel</td> 
      <td><input type="text" name="id_artikel" value="<?php echo $id_artikel; ?>" readonly/></td> 
     </tr> 
     <tr> 
      <td>Judul</td> 
      <td><textarea name="judul"><?php echo $judul; ?></textarea></td> 
     </tr> 
     <tr> 
      <td>Kategori</td> 
      <td><label> 
      <select name="kategori" class="textfield" id="id_kategori"> 
      <option id="0">--Pilih Kategori--</option> 
      <?php 
      foreach ($kategori as $a) { ?> 
      <option <?php if($kategori_terpilih == $a->id_kategori) echo 'selected = "selected"'; ?> value="<?php echo $a->id_kategori ?>" <?php echo $a->nama_kategori ?>></option>; 
      <?php 
      } 
      ?> 
     </select> 
     </tr> 
     <tr> 
      <td>Isi</td> 
      <td><textarea name="isi"><?php echo $isi ?></textarea></td> 
     </tr> 
     <tr> 
      <td>Foto</td> 
      <td><input type="file" name="foto" size="20" /><?php echo $foto ?></td> 
     </tr> 
     <tr> 
      <td> 
      </td> 
      <td><input type="submit" name="btnSubmit" value="Simpan"/></td> 
     </tr> 

    </table> 
    </form> 
</body> 

回答

4

從CodeIgniter的documentation

By default the upload routine expects the file to come from a form field called userfile, and the form must be of type 「multipart」.

所以,要麼使用name="userfile" name屬性上的文件上傳字段,或者使用$this->upload->do_upload('foto')

也把enctype="multipart/form-data"表單上指定的領域。

+0

好的謝謝。這行得通 –