如何使用codeigniter编辑数据库中的数据

时间:2013-12-01 21:48:00

标签: php mysql forms codeigniter

我想知道如何选择数据库表中的选民,并使用Codeigniter中的表单编辑信息。我已经为它创建了一个搜索表,并将在下面发布代码。

型号:

public function search_voters($search_term){
   $this->db->select('*');
   $this->db->from('voterinfo');
   $this->db->like('firstName', $search_term['firstName']);
   $this->db->like('lastName', $search_term['lastName']);
   $this->db->like('street', $search_term['street']);
   $this->db->like('dob', $search_term['dob']);
   $query = $this->db->get();
   return $query->result_array();

}

控制器:

public function search(){
    $this->load->model('reg_model');

    $search_term = array(
    'firstName' => $this->input->post('firstName'),
    'lastName' => $this->input->post('lastName'),
    'street' => $this->input->post('street'),
    'dob' => $this->input->post('dob')
        );


    $data['query'] = $this->reg_model->search_voters($search_term);


    $this->load->view("reg_header");
    $this->load->view("reg_nav");
    $this->load->view("reg_search", $data); 
}

查看:

echo form_open("reg/search");

echo form_label("First Name: ", "firstName");

$data = array(
    "name" => "firstName",
    "id" => "firstName",
    "value" => set_value("firstName")
);    
echo form_input($data);

//last name
echo form_label("Last Name: ", "lastName");

$data = array(
    "name" => "lastName",
    "id" => "lastName",
    "value" => set_value("lastName")
);    
echo form_input($data);

echo form_label("Street: ", "street");

$data = array(
    "name" => "street",
    "id" => "street",
    "value" => set_value("street")
);    
echo form_input($data);

echo form_label("Date of Birth: ", "dob");

$data = array(
    "name" => "dob",
    "id" => "dob",
    "value" => set_value("dob")
);    
echo form_input($data);

echo form_submit("searchSubmit", "Search");

echo form_close();




$this->table->set_heading(array('', 'Voter Number', 'First Name', 'Last Name',
                                'mid Initial', 'Home Number', 'Street',
                                'Apt', 'Zip', 'DOB',
                                'Local District', 'State District'));

foreach ($query as $row){

    $this->table->add_row($row);       
}

echo $this->table->generate();

更新 我一直在努力,想出了一些东西,但我仍然遇到问题 更新后的视图:

foreach ($query as $row){
    $this->table->add_row(anchor('reg/view_form/' ,  $row, 'title="VoterNum"'), $row);

}
echo $this->table->generate();

2 个答案:

答案 0 :(得分:0)

你可能会看到jquery和ajax。如果单击搜索结果中的某一行,则获取带有ajax的新表并使用新数据更新该表。 ajax会将新编辑的信息传递给控制器​​和控制器,使用$htis->input->is_ajax_request()测试从ajax获取数据,并将结果更新到模型中的数据库。

答案 1 :(得分:-1)

假设您有以下内容。

所以你可以这样使用控制器功能。

class Whatever extends CI_Controller {
    function whatever() {
        // use whatever code to update data
    }
}