1
votes

On my codeigniter application, I have a list of users in which an admin can change the role of a user and update the user information. When editing a user the url is user/edit_user/1002 and the edit_user view is loaded. 1002 is the userid of the user.

When I submit the form and form_validation fails I reload the view but the problem is the last segment on the url with the userid is lost. So the sql queries used in the form fails. Is there a way I can reload the view with the errors and also keep the userid in the uri segment ? Given below is the code:

if ($this->form_validation->run() == FALSE) {

        $userid = $this->uri->segment(3);   
        $this->db->where('userid', $userid);
        $query = $this->db->get('user');
        $row1 = $query->row();      
        $data = (array)$row1;                                                   

        $sql = "SELECT userid, role_id FROM user_role WHERE userid = $userid ";// This sql fails
        $ro = $this->db->query($sql);

        $data['role_check'] = $ro->result(); 
        $this->load->view('edit_user',$data);   
}   

Thanks in advance.

In view_user.php,

    <tr>
        <td><?php echo $loop->userid;?></td>
        <td><?php echo $loop->name;?></td>                      
        <td><a class="btn-small btn-info" href="<?php echo site_url(); ?>/user/edit_user/<?php echo $loop->userid;?>">Edit</td>

    </tr>

The in controller user, I have used the code I posted first.

2
set it into SESSION VARIABLE and destroy it after updateJYoThI
or pass the same id again in update action url too user/edit_user/<?php echo $this->uri->segment(3);JYoThI
validation will be false twice i.e. first loading the page and then validating again for error. So, destroying the SESSION VARIABLE doesn't work. The uri segment comes from the view_user page and on clicking EDIT, it goes to edit_user page. If I put <?php echo $this->uri->segment(3); ?> in the action, then the error "Message: Object of class CI_URI could not be converted to string" comes up. This issue has become really irritating.John
can you post your full code what you triedJYoThI
@jothi, updated the postJohn

2 Answers

1
votes

Add the user id in the form's action.

Something like this:

action="user/edit_user/<?php echo $this->uri->segment(3); ?>"
0
votes

I have solved the problem myself and sharing it in case someone else faces this problem. Going through the CI docs, I found that the function returns FALSE if the segment does not exist. I have used it to cover all the conditions that an update may face. The code is given below:

  if($this->uri->segment(3))
        {   $userid = $this->uri->segment(3);   
            $this->session->set_userdata('update_user_id',$userid);
        }               
        else            
            $userid= $this->session->userdata('update_user_id');    

In case of revalidation of the form, $this->uri->segment(3) will return FALSE and the else condition will pick the userid from session which was set when the form was first populated by clicking EDIT link.

Thank you all.