无法上传文件(没有 select 个要上传的文件)
Can't Upload File (not select a file to upload)
我想编辑表格。在编辑表单中,我将上传照片。但是当点击提交按钮时,出现错误:您没有 select 上传文件。有什么问题?
控制器:
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>
来自 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"
作为文件上传字段的名称属性,或者使用 $this->upload->do_upload('foto')
指定字段
同时在您的表单上填写 enctype="multipart/form-data"
。
我想编辑表格。在编辑表单中,我将上传照片。但是当点击提交按钮时,出现错误:您没有 select 上传文件。有什么问题? 控制器:
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>
来自 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"
作为文件上传字段的名称属性,或者使用 $this->upload->do_upload('foto')
同时在您的表单上填写 enctype="multipart/form-data"
。