Inserting data into the database with codeigniter in different tables in the same controller

I have two tables in the database, a form with the fields of table1 and table2! the insertion of table1 fields are occurring normally, however, the insertion of table2 is not occurring!Just don't register!

My controller:

public function cadastrar(){
    esta_logado();
    $this->form_validation->set_rules('nome', 'NOME', 'trim|required|ucwords');
    $this->form_validation->set_rules('email', 'EMAIL', 'trim|valid_email|is_unique[pessoa.email]|strtolower');
    $this->form_validation->set_rules('cpf', 'CPF', 'trim|required');
    $this->form_validation->set_rules('rg', 'RG', 'trim|required');
    $this->form_validation->set_rules('telefone', 'TELEFONE', 'trim|required');
    $this->form_validation->set_rules('rua', 'RUA', 'trim|required');
    $this->form_validation->set_rules('numero', 'NÚMERO', 'trim|required');
    $this->form_validation->set_rules('bairro', 'BAIRRO', 'trim|required');
    $this->form_validation->set_rules('complemento', 'COMPLEMENTO', 'trim|required');
    $this->form_validation->set_rules('cidade', 'CIDADE', 'trim|required');
    $this->form_validation->set_rules('estado', 'ESTADO', 'trim|required');
    if($this->form_validation->run() == TRUE):
        $dados = elements(array('nome', 'email', 'cpf', 'rg', 'telefone'), $this->input->post());
        $this->cidadao->do_insert_cidadao($dados);   
        $dados = elements(array('rua', 'numero', 'bairro', 'complemento', 'cidade', 'estado'), $this->input->post());   
        $this->endereco->do_insert_endereco($dados);    
    endif;

First model:

Class Cidadao_model extends CI_model{

public function do_insert_cidadao($dados = NULL, $redir = TRUE){
    if($dados != NULL):
        $this->db->insert('pessoa', $dados);
        if($this->db->affected_rows() > 0):
        set_msg('msgok', 'Cadastro efeutado com sucesso!', 'sucesso');
        else:
        set_msg('msgerro', 'Erro ao inerir dados!', 'erro');
        endif;
    if($redir) redirect(current_url()); //Irá da um refresh na página
    endif;


}

Second model:

Class Endereco_model extends CI_model{

public function do_insert_endereco($dados = NULL, $redir = TRUE){
    if($dados != NULL):
        $this->db->insert('endereco', $dados);
        if($this->db->affected_rows() > 0):
        set_msg('msgok', 'Cadastro efeutado com sucesso!', 'sucesso');
        else:
        set_msg('msgerro', 'Erro ao inerir dados!', 'erro');
        endif;
    if($redir) redirect(current_url()); //Irá da um refresh na página
    endif;


}

My insert's functions are in different models, I also created the insert's functions in the same model, but it did not work too!

Why can't I insert data into the other table?

Author: Andrew Maxwell, 2014-10-16

1 answers

From what I saw your first insert is redirecting the user after the insert, change the method call as follows

$this->cidadao->do_insert_cidadao($dados, FALSE);  

Because your citizen insert method already has an auto-redirect feature or not.

As a rule, the user can only be redirected or refresh the page after the two inserts.

 2
Author: Fabio Davel, 2014-10-22 13:59:12