How to register multiple

Asked

Viewed 31 times

0

Good evening, I have a dynamic form that sends the following form:

plano_nome: Unimed                     //nome do plano

tipo_especializacao[]: protese         // 1º tipo de especializaçao do plano
plano_tratamento[]: Teste 1            // 1º tratamento do plano
plano_tratamento_valor[]: 23           // 1º valor
plano_usar[]: on                       // se esta ativo ou não  

tipo_especializacao[]: protese         // 2º tipo de especializaçao do plano
plano_tratamento[]: Teste 2            // 2º tratamento do plano
plano_tratamento_valor[]: 32           // 2º valor
plano_usar[]: on                       // se esta ativo ou não 

And I receive in my model with the following code:

public function addPlanos($postData)
{

    $dataPlano = array
    (
        'plano_nome' => $postData['plano_nome']
    );

    $this->db->insert('planos', $dataPlano);
    $insert_id = '';

    if ($this->db->affected_rows() == 1)
    {
        $insert_id = $this->db->insert_id();
        $success = 0;

        $plano_tratamento = explode(",", $postData['plano_tratamento']);
        $plano_tratamento_valor = explode(",", $postData['plano_tratamento_valor']);
        $plano_usar = explode(",", $postData['plano_usar']);
        $tipo_especializacao = explode(",", $postData['tipo_especializacao']);

        for ($i=0; $i < count($plano_tratamento); $i++)
        {
            $data = array
            (
                'plano_tratamento' => trim($plano_tratamento[$i], ','), 
                'plano_tratamento_valor' => trim($plano_tratamento_valor[$i], ','), 
                'plano_usar' => trim($plano_usar[$i], ',') == 'on'? 'S' : 'N', 
                'tipo_especializacao' => trim($tipo_especializacao[$i], ','),
                'id_plano'  => $insert_id 
            );

            $this->db->insert('tratamentos', $data);
            $success += $this->db->affected_rows();
        }

        if ($success > 0)
        {
            return  array('type' => 'success', 'title' => 'Oba!!!', 'text' => 'Plano cadastrado com sucesso!');      
        }
        else
        {
            return  array('type' => 'danger', 'title' => 'Opss!!!', 'text' => 'Erro ao realizar o cadastro! <br> Tente novamente ou entre em contato <br> com o Admistrador do Sistema!');
        }
    }
    else
    {
        return  array('type' => 'danger', 'title' => 'Opss!!!', 'text' => 'Erro ao realizar o cadastro! <br> Tente novamente ou entre em contato <br> com o Admistrador do Sistema!');
    }    
}

However it is giving error, this only registering one, or error in the explode. My logic in the model was to register the plan first (this is happening) and then return your id to register the treatments. In the logic I was looking for he had to register the first and then do the following, but as said is not occurring.

1 answer

0


I believe the problem lies in its variable $insert_id. Since the variable is not being updated in the for loop, when the program tries to insert the second treatment it uses the same id as the previous one, which causes the error.

Try to modify the value of $insert_id in each iteration of the for loop.

  • In vdd it can not be changed friend, because the treatments are of the same plan understood, he registers the plan and returns id so register the treatments.

  • 1

    Ah... then the $insert_id is a foreign key that refers to the plan... got it... It can only help if you send the var_dump of your $Postdata.

Browser other questions tagged

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