Answer the question
In order to leave comments, you need to log in
Do not enter empty field values in the database. Codeigniter, how?
Good day.
It is required not to enter the data of empty fields into the database if the user has not filled in the field.
Controller:
public function settings(){
$this->load->model('Users_Model');
$id = $this->session->userdata('id');
$this->data['get'] = $this->Users_Model->getuser($id);
$password = $this->_clean($this->input->post('password', FALSE));
$email = $this->_clean($this->input->post('email'));
$year = $this->_clean($this->input->post('year'));
if($password == ''){
return $this->load->view('user/settings', $this->data);
} else {
$this->form_validation->set_rules('password', 'Password', 'xss_clean');
$this->form_validation->set_rules('email', 'Email', 'xss_clean|trim');
$this->form_validation->set_rules('year', 'Year', 'xss_clean|required');
if($this->form_validation->run() === FALSE){
$this->data['msg'] = $this->pfunc->error_msg('Введенные вами данные не могут быть обновлены, т.к они не являются валидными.');
return $this->load->view('user/settings', $this->data);
} else {
if($this->Users_Model->update($id, $password, $email, $year)){
$this->data['msg'] = $this->pfunc->error_msg('Данные не были обновлены.');
return $this->load->view('user/settings', $this->data);
} else {
$this->data['msg'] = $this->pfunc->success_msg('Пользователь обновлен!');
return $this->load->view('user/settings', $this->data);
}
}
}
}
public function update($id, $password, $email, $year){
$arr = array(
'password' => sha1(md5($password)),
'email' => $email,
'year' => $year
);
$this->db->where('id', $id);
$this->db->update('users', $arr);
}
Answer the question
In order to leave comments, you need to log in
public function update($id, $password, $email=null, $year=null){
$arr = array(
'password' => sha1(md5($password)),
);
if($email) $arr["email"]=$email;
if($year) $arr["year"]=$year;
$this->db->where('id', $id);
$this->db->update('users', $arr);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question