A database error occurred

Database error saat menambahkan produk. Column 'id_user' cannot be null INSERT INTO `produk` (`id_user`, `id_kategori`, `kode_produk`, `nama_produk`, `slug_produk`, `keterangan`, `keywords`, `harga`, `gambar`, `ukuran`, `status_produk`, `tanggal_post`) VALUES (NULL, '4', 'LA321', 'Line art', 'line-art-la321', ' h \r\n', 'g', '1', 'PicsArt_01-09-07_45_574.jpg', '1', 'Publish', '2020-05-27 11:33:52') Controller produk
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Produk extends CI_Controller{

  //Load model
  public function __construct()
  {
    parent::__construct();
    $this->load->model('produk_model');
    $this->load->model('kategori_model');
    //Proteksi halaman
    //AKTIFKAN NANTI KALAU CSS BISA MUNCUL $this->simple_login->check_login();
  }

  //Data produk
  public function index()
  {
  	$produk= $this->produk_model->listing();

    $data= array( 'title'     => 'Data Produk',
                  'produk'    => $produk,
                  'isi'		    => 'admin/produk/list'
                );
    $this->load->view('admin/layout/wrapper', $data, FALSE);
  }

  //Gambar
  public function gambar($id_produk)
  {
    $produk       = $this->produk_model->detail($id_produk);
    $gambar       = $this->produk_model->gambar($id_produk);

    //Validasi input
    $valid= $this->form_validation;

    $valid->set_rules('judul_gambar','Judul/Nama Gambar','required',
            array('required'  => '%s harus diisi'));

    if($valid->run()) {
     $config['upload_path']     = './assets/upload/image/';
     $config['allowed_types']   = 'gif|jpg|png|jpeg';
     $config['max_size']        = '2400'; //Dalam kb
     $config['max_width']       = '2024';
     $config['max_height']      = '2024';

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

     if ( ! $this->upload->do_upload('gambar')){     
    //End validasi

    $data= array( 'title'      => 'Tambah Gambar Produk: '.$produk->nama_produk,
                  'produk'     => $produk,
                  'gambar'     => $gambar,
                  'error'      => $this->upload->display_errors(),
                  'isi'        => 'admin/produk/gambar'
                );
    $this->load->view('admin/layout/wrapper', $data, FALSE);
    //Masuk database
    }else{
      $upload_gambar = array('upload_data' => $this->upload->data());

      //Create thumbnail gambar
      $config['image_library']    = 'gd2';
      $config['source_image']     = './assets/upload/image/'.$upload_gambar['upload_data']['file_name'];
      //Lokasi folder thumbnail
      $config['new_image']        = './assets/upload/image/thumbs/';
      $config['create_thumb']     = TRUE;
      $config['maintain_ratio']   = TRUE;
      $config['width']            = 250;//dalam ukuran pixel
      $config['height']           = 250;
      $config['thumb_marker']     = '';

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

      $this->image_lib->resize();
      //End create thumbnail

      $i = $this->input;

      $data = array(  'id_produk'        => $i->post('id_produk'),
                      'judul_gambar'     => $i->post('judul_gambar'),                      
                      // Yang disimpan adalah nama file gambarnya
                      'gambar'           => $upload_gambar['upload_data']['file_name'],                     
                    );
          $this->produk_model->tambah_gambar($data);
          $this->session->set_flashdata('sukses', 'Data gambar telah ditambahkan');
          redirect(base_url('admin/produk/gambar/'.$id_produk),'refresh');
      }}
      //End masuk database
       $data= array('title'     => 'Tambah Gambar Produk: '.$produk->nama_produk,
                    'produk'    => $produk,
                    'gambar'    => $gambar,
                    'isi'       => 'admin/produk/gambar'
                );
    $this->load->view('admin/layout/wrapper', $data, FALSE);

  }

  //Tambah produk
  public function tambah()
  {
    //Ambil data kategori
    $kategori = $this->kategori_model->listing();

    //Validasi input
    $valid= $this->form_validation;

    $valid->set_rules('nama_produk','Nama Produk','required',
            array('required'  => '%s harus diisi'));

     $valid->set_rules('kode_produk','Kode Produk','required|is_unique[produk.kode_produk]',
            array('required'  => '%s harus diisi',
                  'is_unique' => '%s sudah ada. Buat kode produk baru'));

    if($valid->run()) {
     $config['upload_path']     = './assets/upload/image/';
     $config['allowed_types']   = 'gif|jpg|png|jpeg';
     $config['max_size']        = '2400'; //Dalam kb
     $config['max_width']       = '2024';
     $config['max_height']      = '2024';

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

     if ( ! $this->upload->do_upload('gambar')){     
    //End validasi

    $data= array( 'title'     => 'Tambah Produk',
                  'kategori'  => $kategori,
                  'error'     => $this->upload->display_errors(),
                  'isi'       => 'admin/produk/tambah'
                );
    $this->load->view('admin/layout/wrapper', $data, FALSE);
    //Masuk database
    }else{
      $upload_gambar = array('upload_data' => $this->upload->data());
      //Create thumbnail gambar
      $config['image_library']    = 'gd2';
      $config['source_image']     = './assets/upload/image/'.$upload_gambar['upload_data']['file_name'];
      //Lokasi folder thumbnail
      $config['new_image']        = './assets/upload/image/thumbs/';
      $config['create_thumb']     = TRUE;
      $config['maintain_ratio']   = TRUE;
      $config['width']            = 250;//dalam ukuran pixel
      $config['height']           = 250;
      $config['thumb_marker']     = '';

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

      $this->image_lib->resize();
      //End create thumbnail

      $i = $this->input;
      //SLUG produk
      $slug_produk = url_title($this->input->post('nama_produk'). '-'.$this->input->post('kode_produk'), 'dash', TRUE);

      $data = array(  'id_user'          => $this->session->userdata('id_user'),
                      'id_kategori'      => $i->post('id_kategori'),
                      'kode_produk'      => $i->post('kode_produk'),
                      'nama_produk'      => $i->post('nama_produk'),
                      'slug_produk'      => $slug_produk,
                      'keterangan'       => $i->post('keterangan'),
                      'keywords'         => $i->post('keywords'),
                      'harga'            => $i->post('harga'),
                      // Yang disimpan adalah nama file gambarnya
                      'gambar'           => $upload_gambar['upload_data']['file_name'],
                      'ukuran'           => $i->post('ukuran'),
                      'status_produk'    => $i->post('status_produk'),
                      'tanggal_post'     => date('Y-m-d H:i:s')
                    );
          $this->produk_model->tambah($data);
          $this->session->set_flashdata('sukses', 'Data telah ditambah');
          redirect(base_url('admin/produk'),'refresh');
      }}
      //End masuk database
       $data= array('title'    => 'Tambah Produk',
                    'kategori'  => $kategori,
                    'isi'       => 'admin/produk/tambah'
                );
    $this->load->view('admin/layout/wrapper', $data, FALSE);
    }

    //Edit produk
    public function edit ($id_produk)
    {
      //Ambil data produk yang akan diedit
      $produk   = $this->produk_model->detail($id_produk);
      //Ambil data kategori
      $kategori = $this->kategori_model->listing();
      //Validasi input      
      $valid    = $this->form_validation;

      $valid->set_rules('nama_produk','Nama Produk','required',
            array('required'  => '%s harus diisi'));

      $valid->set_rules('kode_produk','Kode Produk','required',
            array('required'  => '%s harus diisi'));

      if($valid->run()) {
        //Check jika gambar diganti
        if(!empty($_FILES['gambar']['name'])) {

        $config['upload_path']     = './assets/upload/image/';
        $config['allowed_types']   = 'gif|jpg|png|jpeg';
        $config['max_size']        = '2400'; //Dalam kb
        $config['max_width']       = '2024';
        $config['max_height']      = '2024';

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

      if ( ! $this->upload->do_upload('gambar')) {     
      //End validasi

      $data= array( 'title'   => 'Edit Produk: '.$produk->nama_produk,
                  'kategori'  => $kategori,
                  'produk'    => 'produk',
                  'error'     => $this->upload->display_errors(),
                  'isi'       => 'admin/produk/edit'
                );
      $this->load->view('admin/layout/wrapper', $data, FALSE);
      //Masuk database
      }else{
         $upload_gambar = array('upload_data' => $this->upload->data());

          //Create thumbnail gambar
          $config['image_library']    = 'gd2';
          $config['source_image']     = './assets/upload/image/'.$upload_gambar['upload_data']['file_name'];
          //Lokasi folder thumbnail
         $config['new_image']        = './assets/upload/image/thumbs/';
         $config['create_thumb']     = TRUE;
         $config['maintain_ratio']   = TRUE;
         $config['width']            = 250;//dalam ukuran pixel
         $config['height']           = 250;
         $config['thumb_marker']     = '';

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

      $this->image_lib->resize();
      //End create thumbnail

      $i = $this->input;
      //SLUG produk
      $slug_produk = url_title($this->input->post('nama_produk'). '-'.$this->input->post('kode_produk'), 'dash', TRUE);

      $data = array(  'id_produk'        => $id_produk,
                      'id_user'          => $this->session->userdata('id_user'),
                      'id_kategori'      => $i->post('id_kategori'),
                      'kode_produk'      => $i->post('kode_produk'),
                      'nama_produk'      => $i->post('nama_produk'),
                      'slug_produk'      => $slug_produk,
                      'keterangan'       => $i->post('keterangan'),
                      'keywords'         => $i->post('keywords'),
                      'harga'            => $i->post('harga'),
                      // Yang disimpan adalah nama file gambarnya
                      'gambar'           => $upload_gambar['upload_data']['file_name'],
                      'ukuran'           => $i->post('ukuran'),
                      'status_produk'    => $i->post('status_produk')                     
                    );
          $this->produk_model->edit($data);
          $this->session->set_flashdata('sukses', 'Data telah diedit');
          redirect(base_url('admin/produk/'),'refresh');
      }}else{
        //Edit produk tanpa ganti gambar
          $i = $this->input;
      //SLUG produk
      $slug_produk = url_title($this->input->post('nama_produk'). '-'.$this->input->post('kode_produk'), 'dash', TRUE);

      $data = array(  'id_produk'        => $id_produk,
                      'id_user'          => $this->session->userdata('id_user'),
                      'id_kategori'      => $i->post('id_kategori'),
                      'kode_produk'      => $i->post('kode_produk'),
                      'nama_produk'      => $i->post('nama_produk'),
                      'slug_produk'      => $slug_produk,
                      'keterangan'       => $i->post('keterangan'),
                      'keywords'         => $i->post('keywords'),
                      'harga'            => $i->post('harga'),
                      // Yang disimpan adalah nama file gambarnya (gambar tidak diganti)
                      //'gambar'           => $upload_gambar['upload_data']['file_name'],
                      'ukuran'           => $i->post('ukuran'),
                      'status_produk'    => $i->post('status_produk')                     
                    );
          $this->produk_model->edit($data);
          $this->session->set_flashdata('sukses', 'Data telah diedit');
          redirect(base_url('admin/produk'),'refresh');
      }}
      //End masuk database
       $data= array( 'title'    => 'Edit Produk: '.$produk->nama_produk,
                    'kategori'  => $kategori,
                    'produk'    => $produk,
                    'isi'       => 'admin/produk/edit'
                );
      $this->load->view('admin/layout/wrapper', $data, FALSE);

    }

    //Delete produk
    public function delete($id_produk)
    {
      //Proses hapus gambar
      $produk = $this->produk_model->detail($id_produk);
      unlink('./assets/upload/image/'.$produk->gambar);
      unlink('./assets/upload/image/thumbs/'.$produk->gambar);
      //End proses hapus
      $data = array('id_produk' => $id_produk);
      $this->produk_model->delete($data);
      $this->session->set_flashdata('sukses', 'Data telah dihapus');
      redirect(base_url('admin/produk'),'refresh');
    }

    //Delete gambar produk
    public function delete_gambar($id_produk,$id_gambar)
    {
      //Proses hapus gambar
      $gambar = $this->produk_model->detail_gambar($id_gambar);
      unlink('./assets/upload/image/'.$gambar->gambar);
      unlink('./assets/upload/image/thumbs/'.$gambar->gambar);
      //End proses hapus
      $data = array('id_gambar' => $id_gambar);
      $this->produk_model->delete_gambar($data);
      $this->session->set_flashdata('sukses', 'Data gambar telah dihapus');
      redirect(base_url('admin/produk/gambar/'.$id_produk),'refresh');
    }

  }

Produk model
<?php
defined('BASEPATH') or exit ('No direct script access allowed');

class Produk_model extends CI_Model {

	public function __construct()
 	{
    	parent::__construct();
    	$this->load->database();
	}
	
	//Listing all produk
	public function listing()
	{
		$this->db->select('produk.*,
						users.nama,
						kategori.nama_kategori,
						kategori.slug_kategori,COUNT(gambar.id_gambar) AS total_gambar');
		$this->db->from('produk');
		//JOIN
		$this->db->join('users','users.id_user = produk.id_user', 'left');
		$this->db->join('kategori','kategori.id_kategori = produk.id_kategori', 'left');
		$this->db->join('gambar','gambar.id_produk = produk.id_produk', 'left');
		//END JOIN
		$this->db->group_by('produk.id_produk');
		$this->db->order_by('id_produk', 'desc');
		$query = $this->db->get();
		return $query->result();
	}

	//Detail produk
	public function detail($id_produk)
	{
		$this->db->select('*');
		$this->db->from('produk');
		$this->db->where('id_produk', $id_produk);
		$this->db->order_by('id_produk', 'desc');
		$query = $this->db->get();
		return $query->row();
	}

	//Detail gambar produk
	public function detail_gambar($id_gambar)
	{
		$this->db->select('*');
		$this->db->from('gambar');
		$this->db->where('id_gambar', $id_gambar);
		$this->db->order_by('id_gambar', 'desc');
		$query = $this->db->get();
		return $query->row();
	}

	//Gambar
	public function gambar($id_produk)
	{
		$this->db->select('*');
		$this->db->from('gambar');
		$this->db->where('id_produk', $id_produk);
		$this->db->order_by('id_gambar', 'desc');
		$query = $this->db->get();
		return $query->result();
	}

	//Tambah
	public function tambah($data)
	{
		$this->db->insert('produk', $data);
	}

	//Tambah gambar
	public function tambah_gambar($data)
	{
		$this->db->insert('gambar', $data);
	}

	//Edit
	public function edit($data)
	{
		$this->db->where('id_produk', $data['id_produk']);
		$this->db->update('produk', $data);
	}

	//Delete
	public function delete($data)
	{
		$this->db->where('id_produk', $data['id_produk']);
		$this->db->delete('produk', $data);
	}

	//Delete gambar
	public function delete_gambar($data)
	{
		$this->db->where('id_gambar', $data['id_gambar']);
		$this->db->delete('gambar', $data);
	}

}
Views produk tambah
<?php
//Error upload
if(isset($error)) {
  echo '<p class="alert alert-warning';
  echo $error;
  echo '</p>';
}

//Notifikasi error
echo validation_errors('<div class="alerr alert-warning">','</div>');

//Form open
echo form_open_multipart(base_url('admin/produk/tambah/'),' class="form-horizontal"');
?>

<div class="form-group">
  <label class="col-md-2 control-label">Nama produk</label>
  <div class="col-md-5">
    <input type="text" name="nama_produk" class="form-control" placeholder="Nama Produk" value="<?php echo set_value('nama_produk') ?>" required>
  </div>
</div>

<div class="form-group">
  <label class="col-md-2 control-label">Kode produk</label>
  <div class="col-md-5">
    <input type="text" name="kode_produk" class="form-control" placeholder="Kode Produk" value="<?php echo set_value('kode_produk') ?>" required>
  </div>
</div>

<div class="form-group">
  <label class="col-md-2 control-label">Kategori produk</label>
  <div class="col-md-5">
    <select name="id_kategori" class="form-control">
      <?php foreach($kategori as $kategori) { ?>
      <option value="<?php echo $kategori->id_kategori ?>"> 
        <?php echo $kategori->nama_kategori ?>
      </option>
      <?php } ?>
    </select>
  </div>
</div>

<div class="form-group">
  <label class="col-md-2 control-label">Harga produk</label>
  <div class="col-md-5">
    <input type="number" name="harga" class="form-control" placeholder="Harga Produk" value="<?php echo set_value('harga') ?>" required>
  </div>
</div>

<div class="form-group">
  <label class="col-md-2 control-label">Ukuran produk</label>
  <div class="col-md-5">
    <input type="number" name="ukuran" class="form-control" placeholder="Ukuran Produk" value="<?php echo set_value('ukuran') ?>" required>
  </div>
</div>

<div class="form-group">
  <label class="col-md-2 control-label">Keterangan produk</label>
  <div class="col-md-8">
    <textarea name="keterangan" class="form-control" placeholder="Keterangan Produk" id="editor"><?php echo set_value('keterangan') ?></textarea>
  </div>
</div>

<div class="form-group">
  <label class="col-md-2 control-label">Keyword (untuk SEO Google)</label>
  <div class="col-md-8">
    <textarea name="keywords" class="form-control" placeholder="Keyword (untuk SEO Google)"><?php echo set_value('keywords') ?></textarea>
  </div>
</div>

<div class="form-group">
  <label class="col-md-2 control-label">Upload gambar produk</label>
  <div class="col-md-8">
    <input type="file" name="gambar" class="form-control" required="required">
  </div>
</div>

<div class="form-group">
  <label class="col-md-2 control-label">Status produk</label>
  <div class="col-md-8">
    <select name="status_produk" class="form-control">
      <option value="Publish">Publikasikan</option>
      <option value="Draft">Simpan Sebagai Draft</option>
    </select>
  </div>
</div>


<div class="form-group">
  <label class="col-md-2 control-label"></label>
  <div class="col-md-5">
  <button class="btn btn-success btn-lg" name="submit" type="submit">
  	<i class="fa fa-save"></i> Simpan
  </button>
  <button class="btn btn-info btn-lg" name="reset" type="reset">
  	<i class="fa fa-times"></i> Reset
  </button>    
  </div>
</div>

<?php echo form_close(); ?>
avatar Exmy31

Ditanyakan oleh @Exmy31

28 Kontribusi -1 Poin

Login untuk menanggapi


Jawaban

$this->session->userdata('id_user')
itu udah dicheck ada isinya ? coba di keluarin hasilnya gan
echo $this->session->userdata('id_user')
kalau kosong, ada yg salah pas ngeset userdata nya berati
avatar abyanjksatu

Dijawab oleh @abyanjksatu

330 Kontribusi 169 Poin

  • cara ngeceknya gimana ? Maaf ini baru latian jadi masih bingung - @Exmy31

Login untuk menanggapi


Login untuk gabung berdiskusi