• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to update account when user don't want to update all database field

#1
suppose this is my controller


Code:
class Admin extends CI_Controller{

   public function account(){

       // if user logged in redirect to home page
       if (!isset($this->session->userdata['loggedin']) || $this->session->userdata['userRoll'] != 'admin')
       {
           redirect (base_url());
       }

       if($this->input->method() == "post")
       {

           //store all settings
           $adminfullname =  $this->input->post('admin-fullname');
           $adminemail =  $this->input->post('admin-email');
           $adminpass =  $this->input->post('admin-pass');


           //load the validation library
           $this->load->library('form_validation');

           $this->form_validation->set_rules('admin-fullname', 'Full Name', 'required');
           $this->form_validation->set_rules('admin-email', 'Admin Email', 'required|valid_email|callback__checkMail');



           //check validation error
           if ($this->form_validation->run() == FALSE)
           {


               //get the all settings from database and store it to variable
               $data['site'] = $this->settings_model->SiteSettings();


               $this->load->view("layout/header",$data);
               $this->load->view("admineditaccount",$data);
               $this->load->view("layout/footer",$data);
           }

           else
           {
               //update user account
               $updateAccount = $this->admin_model->updateAccount($adminfullname,$adminemail,$adminpass);

               if($updateAccount){
                   $this->session->set_userdata('username', $fullname);
                   $this->session->set_userdata('email', $adminemail);

                   $data['msg'] ='<div class="alert slert-success">Successfully Updated</div>';

               }else
               {
                   $data['msg'] ='<div class="alert slert-danger">Error in updating</div>';
               }



               //get the all settings from database and store it to variable
               $data['site'] = $this->settings_model->SiteSettings();


               $this->load->view("layout/header",$data);
               $this->load->view("admineditaccount",$data);
               $this->load->view("layout/footer",$data);

           }
       }else
       {
           //get the all settings from database and store it to variable
           $data['site'] = $this->settings_model->SiteSettings();


           $this->load->view("layout/header",$data);
           $this->load->view("admineditaccount",$data);
           $this->load->view("layout/footer",$data);
       }

   }


   //Admin email check
   public function _checkMail($mail){

       $this->db->where('email', $mail);
       $query = $this->db->get('user');


       if($query->num_rows()==1){

           $this->form_validation->set_message('_checkMail', 'The email address already exist.');
           return FALSE;
       }
       else
       {
           return TRUE;
       }
   }

}
here is my model function



Code:
//Update admin account
function updateAccount($adminfullname, $email, $pass){


   $admin_data = array('username' => $adminfullname, 'email' => $email, 'pass' => $pass);


   //i have store user id in my session and called like this

   $this->db->where('id',$this->sessiondata['uid']);
   $query=$this->db->update('user', $admin_data);
   return $query;
}


But the problem is when user just change the username validation error will be occurred because i have set the value of the field on my form


Code:
<div class="form-group">
   <label class="control-label col-xs-12 col-md-3" for="admin-email">Email :</label>
   <div class="col-xs-4">
       <input class="form-control" id="admin-email" name="admin-email" type="email" value="<?php echo $this->session->userdata['email']; ?>" 1required/>
       <?php echo form_error('admin-email'); ?>
   </div>
</div>


Same problem occurred if user change email or username the password field also updated with the value of password textbox.

How can i overcome this error. Should i have to update each field seperatly with seperate form?
Reply

#2
Below is my Controller method for letting users update their details.

I am using ion_auth to update the details, but it's simply a Model wrapper.

PHP Code:
   public function index()
 
   {
 
       $this->form_validation->set_rules('username''Username''trim');
 
       $this->form_validation->set_rules('first_name''First name''required|trim');
 
       $this->form_validation->set_rules('last_name''Last name''required|trim');
 
       $this->form_validation->set_rules('telephone''Telephone''trim');
 
       $this->form_validation->set_rules('email''Email''required|trim|valid_email|callback_new_email_is_unique');
 
       $this->form_validation->set_rules('new_password''New password''trim|xss_clean|matches[confirm_new_password]');
 
       $this->form_validation->set_rules('confirm_new_password''Confirm new password''trim');

 
       $this->form_validation->set_rules('twitter_username''Twitter Username''trim');
 
       
        if 
($this->form_validation->run() == FALSE):
 
           
            $this
->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
 
           
            $this
->load->vars($this->data);
 
           $this->load->view('account/index');
 
       else:
 
           $data = array(
 
               'first_name' => $this->input->post('first_name'),
 
               'last_name' => $this->input->post('last_name'),
 
               'telephone' => $this->input->post('telephone'),
 
               'email' => $this->input->post('email'),
 
               'twitter_username' => $this->input->post('twitter_username')
 
           );
 
       
            if
($this->input->post('new_password') != '' && $this->input->post('confirm_new_password') != ''):
 
               $data['password'] = $this->input->post('new_password');
 
           endif;
 
       
            if
($this->ion_auth->update($this->user->id$data)):
 
               $this->session->set_flashdata('success-message''You have successfully updated your details.');
 
           else:
 
               $this->session->set_flashdata('error-message''Oops, something went wrong. Please try again.');
 
           endif;
 
           
            redirect
($this->session->flashdata('referrer'), 'location');
 
       endif;
 
   
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.