使用2 where子句进行更新

时间:2015-09-29 13:17:50

标签: php codeigniter

我想用2 where子句创建更新函数,这是我的模型代码:

function updateJawabanSensus($where,$where1,$data) {
   $this->db->where('id_jawaban',$where); 
   $this->db->where('id_sensus',$where1); 
   $this->db->update($this->_table, $data); 
   return $this->db->affected_rows(); 
}

控制器代码:

function update_jawaban_sensus() {  

    $id_jawaban = $this->input->post('id_jawaban', TRUE);
    $id_sensus = $this->input->post('id_sensus', TRUE);
    $id_keluarga = $this->input->post('id_keluarga', TRUE);
    $id_pertanyaan_sensus = $this->input->post('id_pertanyaan_sensus', TRUE);
    $jawaban = $this->input->post('jawab_0', TRUE);
    $id_indikator = $this->input->post('idInd_0', TRUE);
    $indikator = $this->input->post('indikator_0');
    $bobot = $this->m_pilihan_jawaban->get_bobot($id_sensus,$jawaban);
    $nilai_bobot = $bobot * $indikator;
        $data = array(
                'id_jawaban' => $id_jawaban,
                'id_sensus' => $id_sensus,
                'id_indikator' => $id_indikator,
                'id_keluarga' => $id_keluarga,
                'id_pertanyaan_sensus' => $id_pertanyaan_sensus,
                'jawaban' => $jawaban,
                'nilai_bobot' => $nilai_bobot
            );
        //echo json_encode($jawaban);
      $result = $this->m_jawaban_sensus->updateJawabanSensus(array('id_jawaban' => $id_jawaban,'id_sensus' => $id_sensus), $data);
     redirect('indikatorkesejahteraan/c_jawaban_sensus/konfirmasi/'.$id_sensus.'/'.$id_keluarga);
}

但是当我点击更新按钮时,数据无法更改...感谢您的关注

2 个答案:

答案 0 :(得分:2)

第一个问题:您没有显示模型中定义$this->_table的位置。 (我假设你在__construct

中设置了这个

第二个问题:你的模型方法需要三个参数:updateJawabanSensus($where,$where1,$data),但你只发送两个:

updateJawabanSensus(array('id_jawaban' => $id_jawaban,'id_sensus' => $id_sensus), $data);

<强>解决方案

Codeigniter将接受数组中的多个where子句。由于您已经有一个数组,只需发送一次,并允许您的模型方法只接受两个参数。

控制器:

$arr = array(
         'id_jawaban' => $id_jawaban,
         'id_sensus' => $id_sensus
       );
$this->m_jawaban_sensus->updateJawabanSensus($arr, $data);

型号:

function updateJawabanSensus($where,$data) {
   $this->db->where($where); 
   $this->db->update($this->_table, $data); 
   return $this->db->affected_rows(); 
}

来源http://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data

答案 1 :(得分:1)

您只从控制器发送参数表格。

$result = $this->m_jawaban_sensus->updateJawabanSensus(array('id_jawaban' => $id_jawaban,'id_sensus' => $id_sensus), $data);

相反,你可以使用两个条件,你只需要做一次。

<强>模型

function updateJawabanSensus($where,$data) {
   $this->db->where($where); 
   $this->db->update($this->_table, $data); 
   return $this->db->affected_rows(); 
}