C
C
Caspergreen2015-10-04 03:38:17
MySQL
Caspergreen, 2015-10-04 03:38:17

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);
        		}
        	}
        }
    }

Model:
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

1 answer(s)
I
Ivan Koryukov, 2015-10-04
@Caspergreen

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 question

Ask a Question

731 491 924 answers to any question