douxian1895 2015-04-09 23:59
浏览 65
已采纳

codeigniter更新方法不起作用

I am new to codeigniter and trying to write an update function to update information in my database. I've gone through a few tutorials but for some reason my code is not working! Any tips or assistance would be greatly appreciated. I'm just testing with two fields right now, name and email, and I get a blank page when I go to my update view.

Here is my model method:

function get_by_id($id){
  return $this->db->get_where('table',array('id'=>$id));
}

function update($id){
 $attributes=array(
    'name'=> $this->input->post('Name'),
    'email'=> $this->input->post('Email')
    );
  return $this->db->where('id',$id)
           ->update('table',$attributes);
}

And here is my relevant controller code:

function edit(){
  $data['row']=$this->Mymodel->get_by_id($id)->result();
  $data['name']= 'Name';
  $data['email']='Email';
  $this->load->view('updateview', $data);
}

function update($id){

  $this->load->model('Mymodel');
  $this->Mymodel->update($id);
  if($this->input->post()){
    redirect('kittens/view/'.$id, 'refresh');
  }

And here is my update view:

<?php echo form_open('kittens/update/')
echo form_hidden('id', $row[0]->id);


foreach($attributes as $field_name){
echo '<p>' . $field_name;
echo form_input($field_name, $row[0]->$field_name) . '</p>';
}
echo form_submit('', 'Update'); 

echo form_close();
?>

Any help would be appreciated! Not sure what specific part is giving me the trouble!

  • 写回答

3条回答 默认 最新

  • duanpai1920 2015-04-10 13:10
    关注

    Use the following template:

    Controller:

    public function updateData(){
    ....
     $data = array(
      "columnName" => $columnValue
     );
     $whereValue = $someNumber;
     $this->model_name->updateTable($data, $wherevalue);
     ...
    }    
    

    Model:

    public function updateTable($data, $whereValue){
     $this->db->where('columnName', $whereValue);
     $this->db->update('tableName', $data);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站