开发者

Codeigniter updating records in db table

I want to change a specific record in the database using codeigniter. The url should be like this mysite.com/users/edit/10. Here the user having id=10 is being edited user开发者_开发问答s is the controller name and edit is a method.

Usually I do in this way

//code of the rest of controller
.....
function edit(){
$uid =$_REQUEST['uid'];
//database update code
}
...

Where a form is being posted deliberately to change the record


You're not clear at all on what you want, I just can give you some pointers. In CI, you don't need (don't have to) use superglobals to access url parameters. It has a native system to manage uri segments, which became automatically accessible without the need to call them; they're available as arguments of the method you're accessing.

So, in a url like yours, mysite.com/users/edit/10, you'll have

Controller:

class Users extends CI_Controller {

 public function edit($uid)
 {
   // $uid is automatically passed to this method and is already available
   // here you do your operations

   //for. ex.
   $this->load->model('user_model');
   $this->user_model->update_user($uid);
 }

}

Model:

  class User_model extends CI_Model {

     function __construct()
     {
       parent::__construct();
     }

     function update_user($id)
     {
       $this->db->where('id',$id);
       $fields = array('field1' => 'value1','field2' => 'value2'...);
       $this->db->update('users',$fields);
     }
    }

If you provide further information I could expand my answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜