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

Asked

Viewed 1,185 times

0

I have two tables in the database, a form with Tabela1 fields and table2! the insertion of the Table 1 fields is occurring normally, however, the insertion of the table2 is not taking place! Simply do not 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 functions of Insert’s are in different models, I also created in the same model the functions of Insert’s, but it didn’t work either!

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

1 answer

2

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

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

Because your citizen insertion method already has an automatic redirect feature or not.

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

  • It worked perfectly, problem was even redirecting!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.