En E el
En E el

Reputation: 37

Update specific user from database using codeigniter

I just created a user profile page. on my user_model, I have coded this but don't know how am I going to set my controller. I'm just a newbie :D anyone can help? :) thanks in advance

public function get_user(){
   $q = $this->db->query("SELECT * FROM tbl_users");
    return $q->result();
}
public function get_user_byid($u_id) {
    $q = $this->db->query("SELECT * FROM tbl_users WHERE u_id = '$u_id'");
    return $q->result();
}    
function update_user($u_id, $data) {
    $this->db->where('u_id', $u_id);
    $this->db->update('tbl_userss', $data);
    return $this->db->insert_id();
}

in my controller

      <?php
           if (!defined('BASEPATH')) {
                exit('No direct script access allowed');
                  }

    class Profile_controller extends CI_Controller{

         public function __construct() {
            parent::__construct();
            $this->load->model('user_model');
            $this->load->library('session');
        }

        public function index(){
            $data['title'] = 'My Profile';
            $data['content'] = 'profile';
            $this->load->view('layout/admin_layout', $data);
        }

         public function update_user_data(){
           $data = array(
               'username'=>$this->input->post('username'),
               'email'=>$this->input->post('email'),
               'password'=>md5($this->input->post('email')),
               'fname'=>$this->input->post('fname'),
               'mname'=>$this->input->post('mname'),
               'lname'=>$this->input->post('lname'),
               'contact_no'=>$this->input->post('contact_no'),
              'address'=>$this->input->post('address')
           );
           $this->user_model->update_user($data);
        }
    }  

Upvotes: 0

Views: 64

Answers (3)

AldoZumaran
AldoZumaran

Reputation: 572

Use this

function update_user($u_id, $data) {
    $this->db->where('u_id', $u_id);
    $this->db->update('tbl_userss', $data);
    return $this->db->affected_rows() == 1 ? $u_id : 0;
}

EDIT:

url:

POST http://www.example.com/mycontroller/update_user_data/5

Controller

public function update_user_data($u_id){
$data = array(
    'username'=>$this->input->post('username'),
    'email'=>$this->input->post('email'),
    'password'=>md5($this->input->post('email')),
    'fname'=>$this->input->post('fname'),
    'mname'=>$this->input->post('mname'),
    'lname'=>$this->input->post('lname'),
    'contact_no'=>$this->input->post('contact_no'),
    'address'=>$this->input->post('address')
);

$this->user_model->update_user($u_id, $data);

}

Upvotes: 1

Dray
Dray

Reputation: 887

you are sending only one parameter here : $this->user_model->update_user($data); while in your model you are receiving two parameters: function update_user($u_id, $data){...}

you never mentioned the $u_id while passing it to the model.

Upvotes: 0

Mind Stand
Mind Stand

Reputation: 1

$data should be an array which contains values of user table 'tbl_users', like

$data['name'] = 'User Name'; $data['age'] = '22'; etc.. etc..

Here the terms name, age are the fields of table 'tbl_users'

Upvotes: 0

Related Questions