Welcome Guest, Not a member yet? Register   Sign In
Form validation fails to prevent data entry to db
#1

[eluser]alex646[/eluser]
Hey guys,

Can somebody help me with the following issue I have:

Form validation fails to prevent data entry to db after lets say I omit required field. I get the form_error messages that field is required but form still inserts data to db which is blank.

Maybe I'm missing something : (

Thanks!
My controller that handles the form validation
Code:
public function newpost(){
        
        $title = $this->input->post('title');
        $post = $this->input->post('post');
        $this->load->model('private/add_post_model');
        $this->add_post_model->add_post($title, $post);
        
        //$this->form_validation->set_error_delimiters('', '');

        $this->form_validation->set_rules('title', 'Title', 'required|trim|min_length[6]');
        $this->form_validation->set_rules('post', 'Post', 'required|trim|min_length[6]');

        
        if ($this->form_validation->run() == FALSE)
        {
        $this->template->set_theme(Settings_model::$db_config['default_theme']);
        $this->template->set_layout('main');
        $this->template->title('Add post');
        $this->process_partial('header', 'header');
        $this->process_partial('footer', 'footer');    
        $this->process_template_build('private/add_post_form');
        
        }
        
            else
        {
        
        $this->template->set_theme(Settings_model::$db_config['default_theme']);
        $this->template->set_layout('main');
        $this->template->title('Add post');
        $this->process_partial('header', 'header');
        $this->process_partial('footer', 'footer');    
        $this->process_template_build('private/form_success');  }
}

My model:
Code:
class Add_post_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }


    public function add_post($title, $post) {

        if ($title!=NULL && $post!=NULL){
       // $uid = $this->_get_userid();  //get current logged in user id from db
        $this->db->set('title', $title);
        $this->db->set('post', $post);
       // $this->db->set('user_id', $uid); // e.g. username = 'administrator'
        $this->db->set('user_id',  $this->session->userdata('id')); // e.g. username = 'administrator'

        $query = $this->db->insert('posts');
        }
        
    }


Messages In This Thread
Form validation fails to prevent data entry to db - by El Forum - 04-28-2013, 08:06 PM
Form validation fails to prevent data entry to db - by El Forum - 04-28-2013, 08:57 PM
Form validation fails to prevent data entry to db - by El Forum - 04-28-2013, 09:13 PM



Theme © iAndrew 2016 - Forum software by © MyBB