简体   繁体   English

如何使用Codeigniter实施CRUD操作?

[英]How to implement CRUD operations using Codeigniter?

I am trying to implement CRUD operation using Codeigniter. 我正在尝试使用Codeigniter实施CRUD操作。 My SQL table consists of ID, Name, Class and Section. 我的SQL表由ID,名称,类和部分组成。 I am having trouble with my edit functionality, here is the code: 我的编辑功能遇到问题,这是代码:

Model: 模型:

function update_records($data){
        $this->db->where('ID',$this->uri->segment(3));
        $this->db->update('tbl_demo',$data);
    }

Controller: 控制器:

function update($id){
        $site_data = $this->site_model->get_record($id);
        $view_data = array ();
        $view_data['site_data'] = $site_data;
        $this->load->view("edit_site" , $view_data);
        $data = array(
            'name'=>$this->input->post('name'),
            'class' => $this->input->post('class'),
            'section' => $this->input->post('section')
            );
        $this->site_model->update_records($data);
}

View: 视图:

<?php echo form_open('site/update');?>
<fieldset class='fieldset'>
    <legend>Updata Records : </legend>
    <label class='labelWidth' for='name'>Name : </label>
    <input type='text' name='name' id='name' value='<?php echo $site_data[0]->Name?>'/></br></br>

    <label class='labelWidth' for='class'>Class : </label>
    <input type='text' name='class' id='class' value='<?php echo $site_data[0]->Class?>'/></br></br>

    <label class='labelWidth' for='section'>Section : </label>
    <input type='text' name='section' id='section' value='<?php echo $site_data[0]->Section?>'/></br></br>

    <center><input class='label' type='submit' value='Submit'/></center>
</fieldset>

This is another main view, from which I am sending the ID of the record I want to edit as follows: 这是另一个主视图,我从中发送要编辑的记录的ID,如下所示:

<td><?php echo anchor("site/update/$row->ID",'Edit')?></td>

I am getting 2 errors as 'Message: Missing argument 1 for Site::update()' and 'Message: Undefined variable: id' after this, my ID remains the same, but Name, Class and Section are emptied. 在此之后,我收到2个错误,分别是“消息:Site :: update()缺少参数1”和“消息:未定义变量:id”,我的ID保持不变,但名称,类和节为空。

your view:: 您的看法:

  <?php echo form_open(uri_string());?>
    <fieldset class='fieldset'>
        <legend>Updata Records : </legend>
        <label class='labelWidth' for='name'>Name : </label>
        <input type='text' name='name' id='name' value='<?php echo $site_data[0]->Name?>'/></br></br>

        <label class='labelWidth' for='class'>Class : </label>
        <input type='text' name='class' id='class' value='<?php echo $site_data[0]->Class?>'/></br></br>

        <label class='labelWidth' for='section'>Section : </label>
        <input type='text' name='section' id='section' value='<?php echo $site_data[0]->Section?>'/></br></br>

        <center><input class='label' type='submit' value='Submit'/></center>
    </fieldset>
<?php echo form_close();?>
function update($id){
        $site_data = $this->site_model->get_record($id);
        $view_data = array ();
        $view_data['site_data'] = $site_data;
        $this->load->view("edit_site" , $view_data);
        $data = array(
            'name'=>$this->input->post('name'),
            'class' => $this->input->post('class'),
            'section' => $this->input->post('section')
            );
        $this->site_model->update_records($data);

        // Redirection

        redirect ( 'here add your controller name' );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM