irfanhans irfanhans - 6 months ago 33
PHP Question

Textbox Insert + Update Different Tabel in Codeigniter?

Question : How do I insert & Update into 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 INTO 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 My 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

Error Img Preview

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' WHERE `id_mynetpoin` = 1

Filename: E:/xampp/htdocs/mynet/application/models/Super_Model.php

Line Number: 64

Answer

$datashould be different depending on which table you want to modify.

Here what's looking odd:

$query = $this->Super_Model->update('t_mynetpoin','id_mynetpoin = '.$id,$post);

You must construct $data from the $post data instead of giving the raw $post to your update method.

Or in case you're not updating the right table -- t_mynetpoin instead of t_log_mynetpoin, you could 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,...