Codeigniter创建一个简单的表单来更新数据库

时间:2013-06-11 22:07:09

标签: codeigniter

嗯,我仍然有理解形式的问题。我看过一些没有运气的导游/视频(请记住我睡得很多)所以我想我可以在这里寻求帮助。

假设我有一个名为Notes的表,在Notes表中我有一个名为“user”和“note”的列

我只想使用表单和提交按钮在Notes表中编辑“note”和“user”。

有人可以将控制器和模型的内容放在一起吗?我知道我要求很多,所以我很感激你的时间。

EDIT1

我的模特:

function edit($id)
{
          $this->db->where('id', $id);
          $this->db->update('company_contacts', $data);

}

控制器:

public function edit($id) { 
            if (isset($_POST["edit"]))
            {
                $this->Notes_model->edit($id);
                $url = "/notes/view/" . $id;
                redirect($url);
            }
        $data['data']  = $this->Notes_model->view($id);
        $this->load->view('templates/header');
        $this->load->view('notes/edit', $data);
        $this->load->view('templates/footer');
   }

2 个答案:

答案 0 :(得分:0)

我就是这样做的。这种方法与我使用帮助程序时的方法略有不同。要使用我的代码,您需要启用input帮助程序。

<强>控制器

public function edit($id) { 
  if (isset($this->input->post('edit'))
  {
    $this->load->model('Notes_model'); // You might have missed out on loading the model

    // Since you wish to edit the note and user fields in your table, you need to get the value from the page you are submitting the request from
    $note = $this->input->post('note');
    $user = $this->input->post('user');

    $data = array(
      'note' => $note,
      'user' => $user
    );

    $this->Notes_model->edit($id, $data);
    $url = "/notes/view/" . $id;
    redirect($url);
  }
  $data['data']  = $this->Notes_model->view($id);
  $this->load->view('templates/header');
  $this->load->view('notes/edit', $data);
  $this->load->view('templates/footer');
}

<强>模型

function edit($id, $data)
{
  $this->db->where('id', $id);
  $this->db->update('company_contacts', $data);
}

答案 1 :(得分:-1)

试试这个。

你的模特应该是

function edit($id)
{
    $data = array(
               'note' => $_POST['note'],
               'user'=> $_POST['user']// I don't think you need this. 
            );

     $this->db->where('id', $id);
     $this->db->update('company_contacts', $data);

}