0
什麼錯在我,結果代碼沒有出現在圖像 this is my result笨上傳圖片無法上傳到destinated目錄
,這我控制器
public function __construct()
{
parent::__construct();
$this->load->model('model_barang');
$this->load->helper('currency_format_helper');
$this->load->library(array('pagination','upload'));
}
public function index()
{
$this->load->view('input');
}
public function tampil(){
$data['barang'] = $this->model_barang->tampil_data()->result();
$this->load->view('view_barang',$data);
}
public function tambah(){
$config['upload_path'] = './gambar/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2000';
$config['max_height'] = '1024';
$this->upload->initialize($config);
if(!$this->upload->do_upload('gambar')){
$gambar="";
}else{
$gambar=$this->upload->file_name;
}
$info=array(
'id'=>$this->input->post('id'),
'jenis'=>$this->input->post('jenis'),
'nama'=>$this->input->post('nama'),
'harga'=>$this->input->post('harga'),
'pemasok'=>$this->input->post('pemasok'),
'gambar'=>$gambar
);
$this->model_barang->input_data($info);
redirect('barang/tampil');
}
public function hapus($id){
$where = array('id' => $id);
$this->model_barang->hapus($where,'barang');
redirect('barang/tampil');
}
public function edit($id){
$where = array('id' => $id);
$data['barang'] = $this->model_barang->edit_data($where,'barang')->result();
$this->load->view('edit',$data);
}
public function update(){
$id = $this->input->post('id');
$jenis = $this->input->post('jenis');
$nama = $this->input->post('nama');
$harga = $this->input->post('harga');
$pemasok = $this->input->post('pemasok');
$data = array(
'jenis' => $jenis,
'nama'=> $nama,
'harga' => $harga,
'pemasok' => $pemasok
);
$where = array(
'id' => $id
);
$this->model_barang->update($where,$data,'barang');
redirect('barang/tampil');
}
這是我的模型
private $table="barang";
public function tampil_data(){
return $this->db->get('barang');
}
public function input_data($jenis){
$this->db->insert($this->table,$jenis);
return $this->db->insert_id();
}
public function hapus($where,$table){
$this->db->where($where);
$this->db->delete($table);
}
public function edit_data($where,$table) {
return $this->db->get_where($table,$where);
}
public function update($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
以及我對這個輸入數據的看法
<form action="<?php echo base_url(). 'index.php/barang/tambah'; ?>" method="post">
<fieldset>
<legend><h3>Tambah Data Barang</h3></legend> <br />
ID Barang <input type="text" name="id" required> <br /> <br />
Jenis Barang<input type="text" name="jenis" required> <br /> <br />
Nama Barang<input type="text" name="nama" required> <br /> <br />
Harga Barang<input type="number" name="harga" required> <br /> <br />
Pemasok<input type="text" name="pemasok" required> <br /> <br />
<input type="file" name="gambar"> <br /> <br />
<button class="tombol">Tambah</button>
</fieldset>
</form>
這是我的觀點,以顯示我的數據
<table style="margin:50px auto;" border="1" width="800" height="300">
<tr>
<th>No</th>
<th>ID</th>
<th>Jenis</th>
<th>Nama</th>
<th>harga</th>
<th>pemasok</th>
<th>Gambar</th>
<th>Aksi</th>
</tr>
<?php
$no = 1;
foreach($barang as $q){
?>
<tr>
<td><?php echo $no++ ?></td>
<td><?php echo $q->id ?></td>
<td><?php echo $q->jenis ?></td>
<td><?php echo $q->nama ?></td>
<td><?php echo currency_format($q->harga) ?></td>
<td><?php echo $q->pemasok ?></td>
<td><img src="<?php echo base_url('./gambar/'.$q->gambar);?>" height="100px" width="100px"></td>
<td><?php echo anchor('barang/hapus/'.$q->id,'Hapus', ['onclick'=>'return confirm(\'Apakah Anda Yakin\')']); ?> | <?php echo anchor('barang/edit/'.$q->id,'Edit') ?></td>
</tr>
<?php } ?>
</table>
,這我的數據庫
幫助出現在數據顯示
的可能的複製[笨圖片上傳不工作(http://stackoverflow.com/questions/2788700/codeigniter-image-upload-not-working) –
確保$ q-> id打印在錨點中('barang/edit /'。q-> id,'編輯') 因爲我檢查了錯誤,這是說缺少$ id。 所以仔細檢查這件事情。因爲你的錯誤在上面的代碼中。 –
我會使用codeIgniter窗體幫助器並使用form_open_multipart(); http://www.codeigniter.com/user_guide/helpers/form_helper.html#form_open_multipart – user4419336