2017-11-09 22 views
1

CodeIgniterに何らかの問題があり、CodeIgniterのレコードのデータを更新できません。 'id_user'は私のuserテーブルの主キーです。私はこの問題を解決しようとしましたが、できませんでした。私は誰かがこの問題を助けることを願っています。どうもありがとう:)Codeigniter - MySQLデータベースのデータを更新できません

私は以下のコードを掲載している:

HTML5コード:

<form action="<?php echo base_url(). 'crud/update'; ?>" method="post"> 
    <input name="id_user" type="text" class="form-control" value="<?php echo $id_usernya ?>"> 
    <div class="form-group"> 
     <label>Alamat :</label> 
     <textarea name="alamat_user" class="form-control" rows="5" placeholder="Alamat" ><?php echo $usernya->alamat_user ?></textarea> 
    </div> 
    <div class="form-group"> 
     <input name="kodepos_user" type="text" class="form-control" value="<?php echo $usernya->kodepos_user ?>"> 
    </div> 
    <div class="form-group"> 
     <input type="hidden" name="provinsi_user"> 
     <select name="provinsi_id_user" class="form-control"> 
      <option value="">Pilih Provinsi</option> 
      <option selected value="<?php echo $usernya->provinsi_user ?>"><?php echo $usernya->provinsi_user ?></option> 
      <?php echo $provinsi ?> 
     </select> 
    </div> 
    <div class="form-group"> 
     <input type="hidden" name="kota_user"> 
     <select name="kota_id_user" class="form-control"> 
      <option value="">Pilih Kota</option> 
      <option selected value="<?php echo $usernya->kota_user ?>"><?php echo $usernya->kota_user ?></option> 
     </select> 
    </div> 
    <input type="submit" class="btn btn-primary" value="Simpan"> 
</form> 

コントローラー:

class Crud extends CI_Controller{ 

function __construct(){ 
    parent::__construct();  
    $this->load->model('m_data'); 
    $this->load->helper('url'); 

} 

function edit($id_user){ 
     $where = array('id_user' => $id_user); 
     $data['user'] = $this->m_data->edit_data($where,'user')->result(); 
     $this->load->view('back/user/v_beranda',$data); 
     redirect('beranda/index'); 
    } 

    function update(){ 
    $id_user = $this->input->post('id_user'); 
    $alamat_user = $this->input->post('alamat_user'); 
    $kodepos_user = $this->input->post('kodepos_user'); 
    $provinsi_id_user = $this->input->post('provinsi_id_user'); 
    $kota_id_user = $this->input->post('kota_id_user'); 

    $data = array(
     'alamat_user' => $alamat_user, 
     'kodepos_user' => $kodepos_user, 
     'provinsi_id_user' => $provinsi_id_user, 
     'kota_id_user' => $kota_id_user 
    ); 

    $where = array(
     'id_user' => $id_user 
    ); 

    $this->m_data->update_data($where,$data,'user'); 
    redirect('beranda/index'); 
} 

モデルコード:

<?php 

class M_data extends CI_Model{ 

    function edit_data($where,$table){  
     return $this->db->get_where($table,$where); 
    } 

    function update_data($where,$data,$table){ 
     $this->db->where($where); 
     $this->db->update($table,$data); 
    } 
} 
+0

何あなたが持っているエラーですか?少なくとも、それがなぜ失敗するのかについてのより良い説明。 –

+0

データレコードを更新できません –

+0

はい、わかります。つまり、現在のコードはどうなりますか?エラーが表示されますか?または、データは編集されずに成功しただけです。 –

答えて

0

初期化をあなたのモデルリあなたは、あなたのコントローラーに何の正確なクエリを確認するために、クエリを印刷する

class M_data extends CI_Model{ 

    public function __construct() { 
     parent::__construct(); 
     $this->load->database(); 
    } 

    function edit_data($where,$table){  
     return $this->db->get_where($table,$where); 
    } 

    function update_data($where,$data,$table){ 
     $this->db->where($where); 
     $this->db->update($table,$data); 
    } 
} 
0

試みをやっていることkeが

$this->db->last_query(): 

を実行取得し、クエリの上にそのエラーのショーを実行すると

0
<?php 

class M_data extends CI_Model{ 

    function edit_data($where,$table){  
     return $this->db->get_where($table,$where); 
    } 

    function update_data($where,$data,$table){ 
     $this->db->where($where); 
     $this->db->update($data,$table); 
    } 
} 
関連する問題