尝试使用 codeigniter 更新一行 (table)

Trying to update a row (table) using codeigniter

我尝试更新 table 中的一行,但没有成功。我的意思是,当我点击 "update" 按钮时,它会转到另一个页面,我得到了上一个 "values",所以我可以编辑它们,最后点击 "save"。

但它在我的程序中不起作用:/..当我单击 "edit" 按钮时,它转到另一个页面,我只有一个字段的先前值(字段学科)。当我点击保存时它没有更新任何东西,它创建了一个新行。

这是我的代码: 索引文件,这里我放了按钮 "edit".

                        <a href='".site_url('Home/editar')."/$record->id'> 
                     <button type='button' class='btn btn-primary'>EDIT</button></a>

我的控制器文件,函数"editar"和"saveupdate":

        public function editar($id){
        $data['carreras'] = $this->Crudmodel->get_carreras();
        $data['record']=$this->Crudmodel->get_id_row($id);
        $this->load->view('editar',$data);
        }
          public function saveupdate(){
            $id=$this->input->post("txtid");
            $txtcarr=$this->input->post("txtcarr");
            $txtmat=$this->input->post("txtmat");
            $txtdesc=$this->input->post("txtdesc");
            $txtcarga=$this->input->post("txtcarga");

        $this->Crudmodel->saveup($txtcarr,$txtmat,$txtdesc,$txtcarga);
         $this->db->where("id", $id);
        redirect('Home/index');

    }

具有函数 "saveup"

的 Crudmodel
    public function saveup($txtcarr, $txtmat, $txtdesc, $txtcarga){

      $data=array(
        'carrera_id'=>$txtcarr,
        'nombre'=>$txtmat,
        'descripcion'=>$txtdesc,
        'carga_horaria'=>$txtcarga

    );

      $this->db->update('materias', $data);

}

这是来自 crudmodel 的 get_id_row:

Function

最后,包含所有字段的 "editar" 文件。

<body>

<div class="container"> 
<div class="row">
<div class="col-md-12">


    <h2 align="center">UPDATE SUBJECTS</h2>
    <form method="post" action='<?php echo site_url('Home/saveupdate'); ?>'>
    <tr>

        <td><input type="text" hidden name="txtid" value="<?php echo $record->id ?>"/></td>

    </tr>
    <tr>

        <td>
            <select name="txtcarr">
                <?php foreach($carreras as $item):?>
                <option value="<?php echo $item->id;?>"><?php echo $item->nombre;?></option>
                 <?php endforeach;?>
            </select>
        </td>
    </tr>
    <tr>

        <td>Subject : </td>
        <td><input type="text" name="txtmat" value="<?php echo $record->nombre ?>"/></td>

    </tr>
    <tr>

        <td>Description : </td>
        <td><textarea name="txtdesc" value="<?php echo $record->descripcion ?>"></textarea></td>

    </tr>
    <tr>

        <td>Hours : </td>
        <td><input type="text" name="txtcarga"/></td>

    </tr>
    <tr>

        <td></td>
        <td><input type="submit" value="Save"/></td>

    </tr>


        <table class="table table-hover" align="center" border="0" cellspacing="0" cellpadding="0" width="300">



        </table>

    </div>
    </div>
    </div>

不明白我该怎么办:S

我认为 Rushabh 认为你应该像这样将你的 $id 和你的数据一起传递给模型:

$this->Crudmodel->saveup($txtcarr,$txtmat,$txtdesc,$txtcarga, $id);

并且在您的 模型中 您可以:

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

并且不要忘记在 控制器;

中删除你的 where 条件

编辑:

您的 get_id_row() 函数需要是:

public function get_id_row($id){
  $query = $this->db->get_where('your_table_name', array('id' => $id));
  return $query->row();
}