php - Textbox Insert + Update Different Tabel in Codeigniter? -
question : how insert & update different tables using textbox
in codeigniter
my database structure
t_mynetpoin
|id_mynetpoin |tot_poin |last_modified| nim |
t_log_mynetpoin
|id_log_mynetpoin | id_paket_redeem | id_mynetpoin | status | total | time | keterangan |
my model
public function get($tabel='',$where='',$order='',$limit='',$from=''){ if (!empty($where)) $this->db->where($where); if (!empty($order)) $this->db->order_by($order); $query = $this->db->get($tabel,$limit,$from); if ($query){ return $query->result(); } else { return array(); } public function insert($tabel,$data){ $query = $this->db->insert($tabel,$data); if($query) return 1; else return 0; } public function update($tabel,$where,$data){ $this->db->where($where); $query = $this->db->update($tabel,$data); if ($query) { return 1; }else{ return 0 ; } }
my controller
public function do_edit($id){ $post = $this->input->post(null, false); $now = date('y-m-d'); $post['last_modified'] = $now; $query = $this->super_model->update('t_mynetpoin','id_mynetpoin = '.$id,$post); $idpr = "2"; $sts= "tambah"; $total = $post['tot_poin']; $post_tot['total'] = $total; $ket = $post['keterangan']; $post_ket['keterangan'] = $ket; $this->super_model->query('insert t_log_mynetpoin (id_paket_redeem, id_mynet_poin, status, total, keterangan) values ($idpr,$id,$sts,$post_tot,$post_ket);'); if($query==1){ $this->session->set_flashdata('success', 'user berhasil di edit!'); redirect("super_admin/user"); }else{ $this->session->set_flashdata('error', 'user gagal di edit!'); redirect("super_admin/user/add"); } }
part view
<form class="form-horizontal" method="post" enctype="multipart/form-data" action="<?php echo site_url("$url")?>"> <p>masukan poin yang akan di tambah</p> <div style="margin-bottom: 10px;"><?php echo form_dropdown("nim", $ambil_nim,@$nim, 'class="form-control" id="nim" disabled="disabled"'); ?></div> <div><input class="form-control col-sm-10" name = "tot_poin" type="text" value = "<?php echo $tpn?>" style="margin-bottom: 10px;"/></div> <div><textarea class="form-control col-sm-10" name="keterangan" placeholder="keterangan menambahkan poin" style="margin-top: 0px;margin-bottom: 0px;height: 100px;"></textarea></div> </div> <div class="modal-footer" style="margin-top: 150px;"> <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> cancel</button> <button type="submit" class="btn btn-primary btn-raised"><i class="fa fa-check"></i> submit</button> </div> </form>
error
a database error occurred error number: 1054 unknown column 'keterangan' in 'field list' update `t_mynetpoin` set `tot_poin` = '8000', `keterangan` = 'hv', `last_modified` = '2016-05-25' `id_mynetpoin` = 1 filename: e:/xampp/htdocs/mynet/application/models/super_model.php line number: 64
$data
should different depending on table want modify.
here what's looking odd:
$query = $this->super_model->update('t_mynetpoin','id_mynetpoin = '.$id,$post);
you must construct $data
$post
data instead of giving raw $post
update method.
or in case you're not updating right table -- t_mynetpoin
instead of t_log_mynetpoin
, try:
$query = $this->super_model->update('t_log_mynetpoin','id_mynetpoin = '.$id,$post);
alternate solution:
consider writing complete query in both cases:
$query = $this->super_model->update('t_mynetpoin','id_mynetpoin = '.$id,$post);
thusly becomes:
$this->super_model->query('update t_mynetpoin set tot_poin = $idpr,...
Comments
Post a Comment