通过Codeigniter |更新记录


Updating Records through Codeigniter |

我创建了一个表来插入Books的数据,其值取自表单。现在我想执行CRUD操作。但我在更新数据时遇到了问题。输入的数据将作为新条目,而不是替换旧数据。我该怎么办?

我所做的如下:

我在主页上创建了一个链接,该链接将重定向到一个页面,其中显示书籍列表及其详细信息,可以通过另一个添加书籍的链接输入。Book_ID已被用作库表的主键。

现在,在显示列表的页面中,我添加了一个"编辑"和一个"删除"按钮。Delete按钮工作良好,因为语法直接而简单。但对于"编辑"按钮,它重定向到"添加图书"页面,但表单使用$Book_ID=$B$this->->uri->segments[3]自动填充特定数据;我已经将其发送到base_ mode以从具有该特定Book_。

现在的问题是,在编辑数据后,我放了一个标签。。。但我不太确定我是否必须这么做。。有人告诉我它一定在那里。但事实上,我实际上必须将Book_ID发送回控制器,并且应该将其发送到模型,在该模型中,它使用该特定的ID来更新该行中的数据。我真的很困惑!

是的,你必须将book_id发送回update records。你可以用这种方式

您的控制器

`public function update_books($book_id)
{
   $data=$this->Your_model_name->book_data($book_id);
   if($data)
   {
       'your success message';
   }
    else
   {
       'Your error message';
   }
}
`

现在在模型

`
public function book_data($book_id)
{
   $this->db->where('id',$book_id);
  if( $this->db->update('TABLE_NAME',array('field_name'=>$this->input->post('post_variable_name'))))
    return true;
  else
     return false;
}
`

这是一种更新记录的简单方法。

将数据作为要更新的记录的数组和id发送。

您的控制器

$form_data = array('name'=>$this->input->post('name'),
                   'email'=>$this->input->('email')
                  );
$this->crud_model->update($form_data,$id);

您的型号:

function update($data,$id)
{
   $this->db->trans_start();
   $this->db->where('id',$id);
   $this->db->update('yourtablename',$data);
   $this->db->trans_complete();
   return TRUE;
}

这样做就会奏效。