简体   繁体   中英

How to upload file codeigniter to database

I have tried but it always appears error unable to load the upload class.

 function save()
 {

    $id_pemesanan=$this->input->post('id_pemesanan');
    $nama_pemesan=$this->input->post('nama_pemesan');
    $email=$this->input->post('email');
    $jumlah=$this->input->post('jumlah');
    $tgl_bayar=$this->input->post('tgl_bayar');
    $metode_pembayaran=$this->input->post('metode_pembayaran');

    $data['id_pemesanan'] = $id_pemesanan;
    $data['nama_pemesan'] = $nama_pemesan;
    $data['email'] = $email;    
    $data['jumlah'] = $jumlah;
    $data['tgl_bayar'] = $tgl_bayar;
    $data['metode_pembayaran'] = $metode_pembayaran;

    $this->db->trans_start();

    $this->db->insert('konfirmasi', $data);

    $this->db->trans_complete();  



    if ($this->db->trans_status() === FALSE)
            {
                $this->session->set_flashdata("msg", "<div class='alert bg-danger' role='alert'>
                <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                <svg class='glyph stroked empty-message'><use xlink:href='#stroked-empty-message'></use></svg> Data gagal tersimpan.
                </div>");
                redirect('konfirmasi/konfirmasi_list'); 
            } else 
            {
                $this->session->set_flashdata("msg", "<div class='alert bg-success' role='alert'>
                <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                <svg class='glyph stroked empty-message'><use xlink:href='#stroked-empty-message'></use></svg> Data  tersimpan.
                </div>");
                redirect('konfirmasi/konfirmasi_list'); 
            }

 }

this is my model

public function datakonfirmasi()
{
$query = $this->db->query('SELECT * FROM konfirmasi');
return $query->result();
}

and this is my views

<div class="row">
  <ol class="breadcrumb">
    <li><a href="#"><svg class="glyph stroked home"><use xlink:href="#stroked-home"></use></svg></a></li>
    <li class="active">konfirmasi</li>
  </ol>
</div>
<!--/.row-->
<br>
<div class="row">
  <div class="col-lg-12">
    <div class="panel panel-default">
      <div class="panel-heading"><svg class="glyph stroked male user "><use xlink:href="#stroked-male-user"/></svg>
        <div class="panel-body">

          <div class="col-md-6">
            <form method="post" action="<?php echo base_url();?><?php echo $url;?>" enctype="multipart/form-data">

              <input type="hidden" class="form-control" name="id_konfirmasi" value="<?php echo $id_konfirmasi;?>">

              <div class="col-sm-5">
                <label>Kode Pemesanan</label>
                <input type="id_pemesanan" name="id_pemesanan" class="form-control" required="required" placeholder="Kode Pemesanan">
                <br>
              </div>

              <div class="col-sm-5">
                <label>Nama Pemesan</label>
                <input type="nama_pemesan" name="nama_pemesan" class="form-control" required="required" placeholder="Masukan Nama..">
                <br>
              </div>

              <div class="col-sm-5">
                <label>Email</label>
                <input type="email" name="email" class="form-control" required="required" placeholder="Masukan Email..">
                <br>
              </div>

              <div class="col-sm-5">
                <label>Jumlah Pembayaran</label>
                <input type="jumlah" name="jumlah" class="form-control" required="required" placeholder="Masukan Jumlah Pembayaran">
                <br>
              </div>

              <div class="col-sm-5">
                <label>Tanggal Bayar</label>
                <input type="date" name="tgl_bayar" class="form-control" required="required" placeholder="Tanggal Bayar">
                <br>

                <label>METODE PEMBAYARAN</label>
                <select name="metode_pembayaran" id="metode_pembayaran" class="form-control">
                  <option value="BNI">BNI</option>
                  <option value="MANDIRI">MANDIRI</option>
                  <option value="BCA">BCA</option>
                </select>
              </div>
              <br>
              <div class="col-sm-5">
                <label>Bukti Transfer</label>
                <input type="file" name="gambar" required>
              </div>

              <div class="col-sm-5">
                <br>
                <br>
                <br>
                <a href="<?php echo base_url();?>konfirmasi/konfirmasi_list" class="btn btn-default">Batal</a> &nbsp &nbsp &nbsp &nbsp
                <button type="submit" class="btn btn-primary">Simpan</button>
              </div>

            </form>


          </div>
        </div>
      </div>
    </div>
    <!--/.row-->

How to upload image files?

Saving files in database isn't a good practice. You should save file to some folder with some name and store only path to this file in DB. Whenever you need this file you just get it from your filesystem - because you know path to this file

save the files in a folder with in your application folder in code-igniter in htdocs and save the path in database. that would be a good practic.

try this:

in your controller

function save(){
//add yourmodel
$this->load->model('Yourmodels');

$id_pemesanan=$this->input->post('id_pemesanan');
$nama_pemesan=$this->input->post('nama_pemesan');
$email=$this->input->post('email');
$jumlah=$this->input->post('jumlah');
$tgl_bayar=$this->input->post('tgl_bayar');
$metode_pembayaran=$this->input->post('metode_pembayaran');
$this->db->trans_start();

change

$this->db->insert('konfirmasi', $data);

to:

// call function insert_pemesanan on yourmodels
$this->Yourmodels->insert_pemesanan($data);


$this->db->trans_complete();

in Yourmodels add function insert_pemesanan

function insert_pemesanan($value = []){
$this->db->insert('konfirmasi',$value);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM