更新CodeIgniter中的唯一数据时出错

时间:2016-08-16 03:24:35

标签: php codeigniter

我正在使用CodeIgniter。我无法更新数据,因为我使数据库字段是唯一的。

我的控制器:

function simpanadditional(){
   $this->form_validation->set_rules('nama','Nama Additional','trim|required');
    $this->form_validation->set_rules('harga','Harga Additional','required');
    if($this->form_validation->run() == FALSE){
        redirect('tambah-additional','refresh');
    }else{
        $cek = $this->my_model->ceknamadditional($this->input->post('nama'));
        if(count($cek) > 0){
            $this->session->set_flashdata('message','nama tidak boleh sama');
            redirect('tambah-additional','refresh');
        }else{
            $this->my_model->simpanadditional();
            redirect('admin/tabel_additional','refresh');
        }   
    }
}

function editadditional($id){
    if($this->session->userdata('logged_in'))
    {
        $session_data = $this->session->userdata('logged_in');
        $data['username'] = $session_data['username'];
        $data['title'] = 'Edit Additional';
        $data['main'] = 'admin/additional/form_edit_additional';
        $data ['getdata'] = $this->my_model->editadditional($id);
        $data['getgroup'] = $this->my_model->editgroup($session_data['username']);
        $this->load->view('admin/index',$data);
    } else {
        redirect('admin/login', 'refresh');
    }
}

我的模特:

function simpanadditional(){
    $data = array(
        'nama_additional'   => $this->input->post('nama'),
        'harga_additional'  => $this->input->post('harga'),
        );
    if($this->input->post('id')){
        // ini untuk update nya
        $this->db->where('id_additional',$id);
        $this->db->update('additional',$data);
    }else{
        // ini untuk insert nya
        $this->db->insert('additional',$data);
    }

}

function editadditional($id){
    $data = array();
    $this->db->select('*');
    $this->db->from('additional');
    $this->db->where('id_additional',$id);
    $hasil = $this->db->get();
    if($hasil->num_rows() > 0){
        $data = $hasil->result();
    }

    $hasil->free_result();
    return $data;
}

这是附加形式的视图:

 <div class="item form-group">
      <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Nama <span class="required">*</span>
      </label>
      <div class="col-md-6 col-sm-6 col-xs-12">
        <input id="name" class="form-control col-md-7 col-xs-12" data-validate-length-range="6" data-validate-words="2" name="nama" value="<?php echo $data->nama_additional ?>" type="text">
        <input type="hidden" name="id" value="<?php echo $data->id_additional ?>">
      </div>
</div>
<div class="item form-group">
  <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Harga <span class="required">*</span>
  </label>
   <div class="col-md-6 col-sm-6 col-xs-12">
  <input id="icon" class="form-control col-md-7 col-xs-12" data-validate-length-range="6" data-validate-words="2" 
  name="harga" value="<?php echo $data->harga_additional ?>" type="text"  onkeypress="return isNumberKey(event)">
  </div>
</div>

有人可以帮忙吗?

0 个答案:

没有答案