更新多行CodeIgniter

时间:2016-04-15 07:18:44

标签: php codeigniter

我想更新我的数据库,但是当我点击“保存”按钮并重定向到索引时。它没有改变任何东西。 这是我的控制器:

public function update_agenda() {
                for($i = 1; $i < count($this->input->post('did')); $i++) {
                    if($this->input->post('did')[$i]!= '' ){
                        $data = array (
                    'nama' => $this->input->post('dnama'),
                    'keterangan' => $this->input->post('dketer') );
                $id= $this->input->post('did');
                $this->load->model('agenda_model');

                    $where= array('id'=> $id);
            $res = $this->agenda_model->UpdateData('agenda',$data,$where);
            if($res >=1){
            redirect ('ajaxsample/index');
            }
}

模型

public function UpdateData($tabelName,$data,$where){
        $res =$this->db->update_batch($tabelName,$data,$where);

这是我的观点。当我点击按钮保存时,我不知道为什么。它不起作用。 请帮助我,谢谢:D

<?php foreach ($daftar_agenda as $agenda) { ?> <tr> 

        <td> <input type ="checkbox" id="haha" onclick="#" class=checkbox1 name="checklist" value=<?php echo $agenda->id; ?>> <?php echo $agenda->id; ?> </td>
        <td> <input type="text" id="hide" name="did" value="<?php echo $agenda->id; ?>"> </td>

        <td> <input type="text" name="dnama" id="nama_" class="yes" value="<?php echo $agenda->nama; ?>" disabled /> </td>
        <td> <input type="text" name="dketer" id="ket_" value="<?php echo $agenda->keterangan; ?>" disabled> </td>

  保存  

2 个答案:

答案 0 :(得分:1)

问题#1:在您的视图文件中,您有一个名为“did”的输入。它不是一个数组,因此在您的Controller中,您不能将其称为数组。 [并且最好一次使用“id”标签,或者它必须是唯一的(但这对于javascript函数来说是个问题,在这种情况下它并不重要)]

问题#2:在Model函数中,缺少return参数。 它会更好:

public function UpdateData($tabelName, $data, $where){
  $res = $this->db->where($where)
        ->update($tabelName, $data);

  return $this->db->affected_rows(); // returns the affected rows number, so it needs to be higher than 0, if any row was affected.
}

问题#3:在您的控制器功能中,只需在模块顶部加载模型一次即可。并且您在for方法的第一个$ i之后重定向页面。所以你需要将redirect()放在for方法之外。

使用此解决方案,您的控制器功能应该没问题:

public function update_agenda()
{
    $this->load->model('agenda_model');
    $done = 0;

    for($i = 1; $i < count($this->input->post('did')); $i++)
    {
        if($this->input->post('did')[$i] != '' )
        {
            $data = array(
                'nama' => $this->input->post('dnama'),
                'keterangan' => $this->input->post('dketer')
            );

            $id = $this->input->post('did')[$i];
            $where = array('id'=> $id);

            $res = $this->agenda_model->UpdateData('agenda', $data, $where);
            $done += $res;

        }
    }

    if($done >= 1)
    {
        redirect ('ajaxsample/index');
    }
}

但是对于这个解决方案,你的'确实'输入需要是一个数组!

答案 1 :(得分:0)

 $data=array();     

          if($this->input->post('did')!= '' ){
                     $data[] = array (
                        'nama' => $this->input->post('dnama'),
                        'keterangan' => $this->input->post('dketer'),
                         'id'=>$this->input->post('did'),
                     );

          }

      $this->load->model('agenda_model');
      $res = $this->agenda_model->UpdateData('agenda',$data);
      if($res >=1){
          redirect ('ajaxsample/index');
      }

在模型中

public function UpdateData($tabelName,$data){
        $res =$this->db->update_batch($tabelName,$data);
}