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?