假设这是我的控制器
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;
}
}
}
这是我的模型功能
//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;
}
但问题是当用户只是更改用户名验证时会发生错误,因为我在表单上设置了字段的值
<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>
如果用户更改了电子邮件或用户名密码字段也使用密码文本框的值更新,则会出现同样的问题。
我怎样才能克服这个错误。我是否应该以单独的形式单独更新每个字段?
答案 0 :(得分:0)
我个人不会将帖子数据放在变量中,因为它已经是(个人的首选项),除此之外,你想要检查每个可以更新的字段是否有值,例如:
$data = array();
if(!empty($this->input->post('username'))){
$data['username'] = $this->input->post('username');
}
对每个输入重复此步骤。之后它和你已经拥有的一样
$this->db->where('id',$this->sessiondata['uid']);
$this->db->update("table", $data);