douwei7501 2017-03-31 15:09
浏览 19
已采纳

尝试使用codeigniter更新行(表)

Im trying without success to update a row in a table. I mean, when i click on "update" button it goes to another page and i got the previous "values" so i can edit them and finally click on "save".

But it is not working in my program :/..When i click on "edit" button it goes to another page and i only have the previous values of a single field (Field subject). AND when i click on save it does not update anything, it creates a new row.

Here is my code: The index file, here i put the button "edit".

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

My controller file, with the function "editar" and "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');

    }

The Crudmodel with the function "saveup"

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

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

    );

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

}

Here is the get_id_row from crudmodel:

Function

Finally, the "editar" file which contains all the fields.

<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>

Do not understand what should i do :S

  • 写回答

1条回答 默认 最新

  • dopq87915 2017-03-31 15:29
    关注

    I think Rushabh thinks you should pass your $id with your data to the model like this:

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

    and in your model you can:

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

    and don't forget to remove your where condition in the controller;

    Edit:

    your get_id_row() function needs to be:

    public function get_id_row($id){
      $query = $this->db->get_where('your_table_name', array('id' => $id));
      return $query->row();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?