Welcome Guest, Not a member yet? Register   Sign In
Need help with Update function
#1

[eluser]AdonisSMU[/eluser]
This is the function from my controller.
Code:
function manage_user($id)
    {
        $this->load->model('musers');
        $this->load->model('muserprofile');
        $user_update = $this->input->post('user_update');    
        
        $data['title'] = 'H.P.N. White Label by TEN || User Update Area';
        $data['main_content'] = 'manage_user';
        $data['login'] = $this->musers->getUser($id);
        $data['info'] = $this->muserprofile->getUserProfile($id);
        if ($user_update == true) {
        
            $user['email'] = $data['login']['email'];
            $user['username'] = $data['login']['username'];
            
            $profile['first_name'] = $data['info']['first_name'];
            $profile['last_name'] = $data['info']['last_name'];
            $profile['company_name'] = $data['info']['company_name'];
            $profile['salutation'] = $data['info']['salutation'];
            $profile['title'] = $data['info']['title'];
            $profile['street_address'] = $data['info']['street_address'];
            $profile['city'] = $data['info']['city'];
            $profile['state'] = $data['info']['state'];
            $profile['zip'] = $data['info']['zip'];
            $profile['country'] = $data['info']['country'];
            $profile['phone'] = $data['info']['phone'];
            
            $login_result = $this->musers->updateUser($id, $user);
            $profile_result = $this->muserprofile->updateUserProfile($id, $profile);
            
            if ($login_result && $profile_result) {
                $data['msg'] = "User profile updated.";
                $data['status'] = "success";
            } else {
                $data['msg'] = "Sorry, user profile not updated.";
                $data['status'] = "error";
            }
        } else {
            $data['msg'] = "";
            $data['status'] = "";
        }
        
        $this->load->view('includes/template', $data);
    }


This is the function from my model.
Code:
function updateUser($id, $users)
    {
        $this->db->where('id', $id);
        $this->db->update('users', $users);
        $result = $this->db->affected_rows();
        
        if($result == 1) {
            return true;
        } else {
            return false;
        }
    }

The $result in the updateUser function is 0 for rows affected and I checked the database and the id matches. Can anyone please help me figure out what I am missing here? Thanks!
#2

[eluser]rogierb[/eluser]
If MySQL does not update, for instance when the data is the same, the affected rows is 0;

What you can do to debug is use $this->db->last_query() to get the full query. Then run the query in phpmyadmin or similar to see what the query is doing.
#3

[eluser]AdonisSMU[/eluser]
I did that and the query itself works just fine when I put it in phpmyadmin.
#4

[eluser]AdonisSMU[/eluser]
I figured this one out myself. My controller initialized the variables to be the same thing they were when they first came to the page. So the db thinks that nothing has been affected. I would never had thought about this until I did what rogierb said. Thanks man!

Chris




Theme © iAndrew 2016 - Forum software by © MyBB