i have trouble with uploading an image using codeigniter. i am new in this and didin't have a clue in how to upload image using codeigniter. so i hope any of you guys can help me out
this is my form named form_add.php
<form method="POST" action="<?php echo base_url()."index.php/crud/insert"; ? >">
<table style="width:110%" class="table table-striped table table-bordered table table-hover">
<td>Nama</td>
<td><input type="text" name="nama_produk"/></td>
</tr>
<tr>
<td>Info</td>
<td><input type="text" name="info_produk"/></td>
</tr>
<tr>
<td>Harga</td>
<td><input type="text" name="harga_produk"/></td>
</tr>
<tr>
<td>Stock</td>
<td><input type="text" name="stock"/></td>
</tr>
<?php echo form_open_multipart('upload/do_upload');?>
<tr>
<td>Gambar</td>
<td><input type="file" name="gambar_produk"></td>
</tr>
<tr>
<td>Di Buat Oleh</td>
<td><input type="text" name="penulis_produk"/></td>
</tr>
<tr>
<td>Kategori</td>
<td><input type="text" name="kategori"/></td>
</tr>
<tr>
<td>Kode Kategori</td>
<td><input type="text" name="kode_kategori"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="btnsubmit" value="Simpan" /></td>
</tr>
</table>
</form>
and this is my controller named crud.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Crud extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
$data = $this->mymodel->GetAll();
$this->load->view('view_all_produk',array('data' => $data));
}
public function add_data(){
$this->load->view('form_add');
}
public function insert(){
$nama_produk = $_POST['nama_produk'];
$info_produk = $_POST['info_produk'];
$harga_produk = $_POST['harga_produk'];
$stock = $_POST['stock'];
$penulis_produk = $_POST['penulis_produk'];
$gambar_produk = $_FILES['gambar_produk']['name'];
$gambar_tmp = $_FILES['gambar_produk']['tmp_name'];
$kategori = $_POST['kategori'];
$kode_kategori = $_POST['kode_kategori'];
$data_insert = array(
'nama_produk' => $nama_produk,
'info_produk' => $info_produk,
'harga_produk' => $harga_produk,
'stock' => $stock,
'penulis_produk' => $penulis_produk,
'gambar_produk' => $gambar_produk,
'kategori' => $kategori,
'kode_kategori' => $kode_kategori
);
$res = $this->mymodel->insertData('produk',$data_insert);
if($res >= 1){
$this->session->set_flashdata('pesan','Tambah Data Sukses');
redirect('crud/index');
}else{
echo "<h2>Insert Data Gagal!!!</h2>";
}
}
public function do_upload()
{
$config['upload_path'] = './assets/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 300;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('gambar_produk'))
{
$error = array('error' => $this->upload- >display_errors());
$this->load->view('form_add', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
}
}
and this is the model named mymodel.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mymodel extends CI_Model {
public function GetAll($where="") {
$data = $this->db->query('select * from produk '.$where);
return $data -> result_array();
}
public function insertData($tableName,$data){
$res = $this->db->insert($tableName,$data);
return $res;
}
public function updateData($tableName,$data,$where){
$res = $this->db->update($tableName,$data,$where);
return $res;
}
public function GetKategori($where=""){
$data = $this->db->query('select * from kategori '.$where);
return $data;
}
}
also this is what it shows after i run it
A PHP Error was encountered
Severity: Notice
Message: Undefined index: gambar_produk
Filename: controllers/crud.php
Line Number: 30
Backtrace:
File: C:\\xampp\\htdocs\\genstore\\admin\\application\\controllers\\crud.php Line: 30 Function: _error_handler
File: C:\\xampp\\htdocs\\genstore\\admin\\index.php Line: 315 Function: require_once
A PHP Error was encountered
Severity: Notice
Message: Undefined index: gambar_produk
Filename: controllers/crud.php
Line Number: 31
Backtrace:
File: C:\\xampp\\htdocs\\genstore\\admin\\application\\controllers\\crud.php Line: 31 Function: _error_handler
File: C:\\xampp\\htdocs\\genstore\\admin\\index.php Line: 315 Function: require_once
A Database Error Occurred
Error Number: 1048
Column 'gambar_produk' cannot be null
INSERT INTO produk
( nama_produk
, info_produk
, harga_produk
, stock
, penulis_produk
, gambar_produk
, kategori
, kode_kategori
) VALUES ('sada', 'asdas', 'asdasd', 'sdasd', 'sdasd', NULL, 'sadasd', '14')
Filename: C:/xampp/htdocs/genstore/admin/system/database/DB_driver.php
Line Number: 691
so can anyone tell me what's my mistake? thanks
Are you using multipart on the form?
<?php echo form_open_multipart('upload/do_upload');?>
https://www.codeigniter.com/userguide3/libraries/file_uploading.html
Edit: i see you are, there's a form within a form in your code!!
try this ! Send form-data encoded as "multipart/form-data":
<form method="POST" action="<?php echo base_url()."index.php/crud/insert"; ?>" enctype="multipart/form-data">
<table style="width:110%" class="table table-striped table table-bordered table table-hover">
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.