duandi4238 2016-07-16 10:26
浏览 223
已采纳

$ this-db-update工作但数据库没有改变

I am currently working on a program that uses php jquery and bootstrap, the update function, there is a an error so that database does not change but the script is running normally, Maybe somebody able to help me where is my mistake? thank you

My controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Subbidang extends CI_Controller {

public function index()
{
    $this->load->model('model_subbidang');
    $this->model_security->getsecurity();
    $isi['content'] = 'Subbidang/tampilan_datasubbidang';
    $isi['judul'] = 'Master';
    $isi['sub_judul'] = 'Sub Bidang'; 
    $isi['data'] = $this->db->get('tsubbidang');
    $this->load->view('tampilan_home',$isi);

}

public function tambah()
{
    $this->load->model('model_subbidang');
    $this->model_security->getsecurity();
    $isi['content'] = 'Subbidang/form_tambahsubbidang';
    $isi['judul'] = 'Master';
    $isi['sub_judul'] = 'Sub Bidang'; 
    $this->load->view('tampilan_home',$isi);
}

public function simpan()
{
    $this->model_security->getsecurity();
    $this->load->model('model_subbidang');

    $key['IDSubBidang'] = $this->input->post('kode');
    $data['IDSubBidang']    = $this->input->post('kode');
    $data['IDBidang']       = $this->input->post('bidang');
    $data['NamaSubBidang']  = $this->input->post('subbidang');

    $query = $this->db->get_where('tsubbidang',$key);

    if ($query->num_rows()>0)
    {
        $this->db->update('tsubbidang',$key,$data);
        //echo "Data Berhasil Diubah";
        echo "Update";
    }
    else
    {
        $this->db->insert('tsubbidang',$data); 
        //xecho "Data Berhasil Disimpan";
        echo 'Insert';
    }
}

} 

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

My View

<script type="text/javascript">
$(document).ready(function(){


$("#simpan").click(function(){
var string = $("#my-form").serialize();
//alert(string);
$.ajax({
            type    : 'POST',   
            url     : '<?php echo site_url();?>/subbidang/simpan',
            data    : string,
            success : function(data){
                alert(data); 
            } 
        });
    });
});
</script>
<form class="form-horizontal" name="my-form" id="my-form">
<div class="control-group">
    <label class="control-label"bidang</label>
    <div class="controls">
        <select name="bidang" id="bidang">
            <option value="">-Pilih-</option>
            <?php

            $bidang = $this->db->get('tbidang');
            foreach ($bidang->result() as $row) {

            ?>
            <option value="<?php echo $row->IDBidang;?>"><?php echo $row->Bidang;?> </option>
            <?php };?>
        </select>
    </div>
</div>

<div class="control-group">
    <label class="control-label">Kode Sub Bidang</label>
    <div class="controls">
        <input type="text" name="kode" id="kode" palceholder="kode" class="span3">
    </div>
</div>

<div class="control-group">
    <label class="control-label">Sub Bidang</label>
    <div class="controls">
        <input type="text" name="subbidang" id="subbidang" palceholder="Sub Bidang" class="span3">
    </div>
</div>

<div class="controls">

    <button type="button" name="simpan" id="simpan" class="btn btn-primary btn-small"> Simpan</button>
</div>

My Model

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_subbidang extends CI_model {

    public function gettampildatasubbidang($key)
    {
        $this->db->where('IDBidang',$key);
        $query = $this->db->get('tbidang');
        if($query->num_rows()>0)
        {
            foreach ($query -> result() as $row) {
                $hasil = $row->Bidang;
            }
        }
        else
        {
            $hasi='';   
        }

        return $hasil;
    }

    public function getlistsubbidang()
    {
        return $this->db->get('tbidang');
    }

    public function getdata($key)
    {
        $this->db->where('IDBidang',$key);
        $hasil = $this->db->get('tBidang');
        return $hasil;
    }

    public function getupdate($key,$data)
    {
        $this->db->where('IDBidang',$key);
        $this->db->update('tBidang',$data);
    }

    public function getinsert($key,$data)
    {
        $this->db->insert('tBidang',$data); 
    }A


}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

The echo work fine but the database doesnt change. The Insert function also work fine

  • 写回答

1条回答 默认 最新

  • doushe7934 2016-07-16 11:56
    关注

    You need to pass right data to update function change

    $this->db->update('tsubbidang',$key,$data);
    

    to

    $this->db->where('IDBidang', $key);
    $this->db->update('tsubbidang',$data);
    

    and Everything will be fine. and one more thing is that you must do functionality related to database in your model not directly in controller, it is not according to the MVC structure. it destort it. and if you done everything seperetly then it is easy to understand and clear to everyone who code after you on the same.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100