Welcome Guest, Not a member yet? Register   Sign In
how update user data using ion_auth ?
#1

[eluser]Keloo[/eluser]
I'm trying update user data for each user using ion_auth update function and I just don't know how.

Here the controller

Code:
function user()
{
  if(!$this->ion_auth->logged_in())
  {
   redirect('home/index');
  }
  else
  {
  
   $this->db->where('username',$this->uri->segment(3));
   $this->data['users'] = $this->ion_auth->users()->result();

   $data = array(
    'first_name' => $this->input->post('first_name'),
    'last_name' => $this->input->post('last_name'),
    'email' => $this->input->post('email')

   );

   $id = $this->input->post('user_id');

   $this->ion_auth->update($id,$data);
   $this->layouts->view('user',$this->data);
  
  }

}

The view
Code:
<?php foreach($users as $user): ?>
<h2>Welcome &lt;?php echo $user->username; ?&gt; </h2>

User info

&lt;?php echo form_open('home/user/'.$user->username); ?&gt;

  <label >First name </label>
          &lt;input type="text" name="first_name" id="first_name" value="&lt;?=$user-&gt;first_name?&gt;" /&gt;
          &lt;?php echo form_error('first_name','<p class="error">','</p>'); ?&gt;
        
          <label for="first_name">Last name </label>
          &lt;input type="text" name="last_name" id="last_name"  value="&lt;?=$user-&gt;last_name?&gt;" /&gt;
          &lt;?php echo form_error('last_name','<p class="error">','</p>'); ?&gt;
                  
          <label for="email_address">Email </label>
          &lt;input type="email" name="email_address" id="email_address"  value="&lt;?=$user-&gt;email?&gt;"/&gt;
          &lt;?php echo form_error('email_address','<p class="error">','</p>'); ?&gt;

          &lt;input type="hidden" name="user_id" id="user_id"  value="&lt;?=$user-&gt;user_id?&gt;" /&gt;
    
    <p>&lt;?php echo form_submit('submit', 'Update');?&gt;</p>

&lt;?php endforeach;?&gt;

When I click update, it updates to 0. I can't figure out how to fix this issue.
#2

[eluser]El J![/eluser]
Try this:

Code:
function user()
{
  if(!$this->ion_auth->logged_in())
  {
   redirect('home/index');
  }

   $id = $this->input->post('user_id');

   // populate data in form
   $this->db->where('username',$this->uri->segment(3));
   $this->data['users'] = $this->ion_auth->users()->result();

  if($id != '') {

    $update = array(
    'first_name' => $this->input->post('first_name'),
    'last_name' => $this->input->post('last_name'),
    'email' => $this->input->post('email')
     );

   $this->ion_auth->update($id,$update);
  
  }

  // Show View
  $this->layouts->view('user',$this->data);

}

Perhaps you'll can print the values of array and $id variable, and check what they have in runtime...

Code:
print_r($update);
echo "<br />" . $id;
die();
#3

[eluser]Keloo[/eluser]
Tried it and got the same problem.
Wanted to update the email, and it updated it to 0.
#4

[eluser]El J![/eluser]
Hi,

In your form the field email address has name "email_address" and in your code you request a POST value "email".... change it! Smile
#5

[eluser]Keloo[/eluser]
Oh, yea thanks for spotting my mistake. :p

It seems that is working now, but it's not displaying the changes after I click updated, I have to refresh the page to seem them.

If I try to redirect I get an error say too many ridercts or something.
#6

[eluser]Rolly1971[/eluser]
the most likely reason the updated data is not populated in the form is because you are populating the '$this->data['users']' variable before a successful update but are not populating it after a successful update.

Code:
function user()
{
  if(!$this->ion_auth->logged_in())
  {
   redirect('home/index');
  }

   $id = $this->input->post('user_id');

   // populate data in form
   $this->db->where('username',$this->uri->segment(3));
   $this->data['users'] = $this->ion_auth->users()->result();

  if($id != '') {

    $update = array(
    'first_name' => $this->input->post('first_name'),
    'last_name' => $this->input->post('last_name'),
    'email' => $this->input->post('email')
     );

     if ($this->ion_auth->update_user($id,$update))
     {
         $this->data['users'] = $this->ion_auth->get_user($id);
     }
  
  }

  // Show View
  $this->layouts->view('user',$this->data);

}
#7

[eluser]El J![/eluser]
Yeah, that's right... but something like this is not better?

Code:
function user()
{
  if(!$this->ion_auth->logged_in())
  {
   redirect('home/index');
  }

   $id = $this->input->post('user_id');

  if($id != '') {

    $update = array(
    'first_name' => $this->input->post('first_name'),
    'last_name' => $this->input->post('last_name'),
    'email' => $this->input->post('email')
     );

     if ($this->ion_auth->update_user($id,$update))
     {
         // maybe set a flash data session, and show a success message
     }
  
  }

  // populate data in form
   $this->db->where('username',$this->uri->segment(3));
   $this->data['users'] = $this->ion_auth->users()->result();

  // Show View
  $this->layouts->view('user',$this->data);

}
#8

[eluser]Keloo[/eluser]
I did something like this:
Code:
function user()
{
  if(!$this->ion_auth->logged_in())
  {
   redirect('home/index');
  }
  
  $id = $this->input->post('id');

  if($id != '') {
   $update = array(
    'first_name' => $this->input->post('first_name'),
    'last_name' => $this->input->post('last_name'),
    'email' => $this->input->post('email')
   );

   if($this->ion_auth->update($id,$update))
   {
    $this->session->set_flashdata('message', "Signup Successful");
    redirect('home/user/'.$this->session->userdata('username'));
   }
  
  }

  $this->db->where('username',$this->uri->segment(3));
  $this->data['users'] = $this->ion_auth->users()->result();
  
  $this->layouts->view('user',$this->data);
  
}

It works fine. Thx for the help guys, much appreciate it. Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB