[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 <?php echo $user->username; ?> </h2>
User info
<?php echo form_open('home/user/'.$user->username); ?>
<label >First name </label>
<input type="text" name="first_name" id="first_name" value="<?=$user->first_name?>" />
<?php echo form_error('first_name','<p class="error">','</p>'); ?>
<label for="first_name">Last name </label>
<input type="text" name="last_name" id="last_name" value="<?=$user->last_name?>" />
<?php echo form_error('last_name','<p class="error">','</p>'); ?>
<label for="email_address">Email </label>
<input type="email" name="email_address" id="email_address" value="<?=$user->email?>"/>
<?php echo form_error('email_address','<p class="error">','</p>'); ?>
<input type="hidden" name="user_id" id="user_id" value="<?=$user->user_id?>" />
<p><?php echo form_submit('submit', 'Update');?></p>
<?php endforeach;?>
When I click update, it updates to 0. I can't figure out how to fix this issue.
[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();
[eluser]Keloo[/eluser]
Tried it and got the same problem.
Wanted to update the email, and it updated it to 0.
[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!
[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.
[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);
}
[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);
}
[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.
|