Jump to content

foreach into database


gmc1103

Recommended Posts

Hello

I'm having a problem using foreach to send records into database

This is my code

 foreach ($idEscola as $id_escola) {
            echo $id_escola."<br>";
        }
        if($getDisponibilidadeData ==0){
            foreach ($idEscola as $id_escola) {
                try {
                    $stmt = $this->db->prepare("INSERT INTO atividades (Atividade,Periodo,Mes,DataPrevista,Hora, Duracao, Destinatarios,Organizador,Orcamento,
                                            Obs,idProjeto,idEscola,idProfessor,idAnoEscolar, Criado)
                                            VALUES(:Atividade,:Periodo,:Mes,:DataPrevista,:Hora,:Duracao,:Destinatarios,:Organizador,:Orcamento,:Obs,:idProjeto,
                                                    :idEscola,:idProfessor,:idAnoEscolar, NOW());");
                    $stmt->bindparam(":Atividade", $novaAtividade);
                    $stmt->bindparam(":Periodo", $periodo);
                    $stmt->bindparam(":Mes", $previsto);
                    $stmt->bindparam(":DataPrevista", $previsao);
                    $stmt->bindparam(":Hora", $hora);
                    $stmt->bindparam(":Duracao", $duracao);
                    $stmt->bindparam(":Destinatarios", $destinatarios);
                    $stmt->bindparam(":Organizador", $organizador);
                    $stmt->bindparam(":Orcamento", $orcamento);
                    $stmt->bindparam(":Obs", $observacoes);
                    $stmt->bindparam(":idProjeto", $id_projeto);
                    $stmt->bindparam(":idEscola", $id_escola);
                    $stmt->bindparam(":idProfessor", $id_professor);
                    $stmt->bindparam(":idAnoEscolar", $id_ano);
                    $dominio = explode(',', $dominios);
                    $result = $stmt->execute();
                    $idAtividade = $this->db->lastInsertId();
                    foreach ($dominio as $idDominios) {
                        $stmt->bindParam(':idDominios', $idDominios, PDO::PARAM_INT);
                        $this->setTabelaAtividadesDominios($idAtividade, $idDominios);
                    }
                    $dpt = $this->getUserDepartamentoById($id_professor);
                    $this->setTabelaDepartamentosAtividades($dpt, $idAtividade);
                    $escola = $this->getEscolaById($id_escola);
                    $professor = $this->getNomeById($id_professor);
                    $nome_dominios = $this->getNomeDominios($idAtividade);
                    $nome_projeto = $this->getNomeProjetos($id_projeto);
                    if ($nome_projeto == '') {
                        $nome_projeto = "Sem Projeto/Clube associado";
                    }
                    $this->AtividadeProjetoPdf($idAtividade, $escola, $nome_projeto, $novaAtividade, $periodo, $previsao, $previsto, $nome_dominios, $destinatarios, $organizador, $orcamento, $observacoes, $professor);
                    if (!$result) {
                        print_r($stmt->errorInfo());
                        return array('status' => 'error', 'message' => 'Not done...');
                    } else {
                        return array('status' => 'success', 'message' => 'Record done...');
                    }

                } catch (PDOException $ex) {
                    echo $ex->getMessage();
                }
            }
        }
        else{
            return array('status' => 'error', 'message' => 'OK ');
            }
        

So...to test if the first foreach is working, i did a test and i receive the values like expected.

1
2
3

The problem is when i enter into the try catch only the first value is inserted into the database

1

 

I should have 3 records and i have only one

 

Any help?

 

Thanks

Link to comment
Share on other sites

Hi Barand

The first loop

foreach ($idEscola as $id_escola) {
            echo $id_escola."<br>";
        } 

Is was just to test if i was receiving the array correctly and yes it works

But then i have the code (remain) to insert into the database and  there...the code is only executed one time .....and it should be 3 times

Remain code to insert into database

if($getDisponibilidadeData ==0){
            foreach ($idEscola as $id_escola) {
                try {
                    $stmt = $this->db->prepare("INSERT INTO atividades (Atividade,Periodo,Mes,DataPrevista,Hora, Duracao, Destinatarios,Organizador,Orcamento,
                                            Obs,idProjeto,idEscola,idProfessor,idAnoEscolar, Criado)
                                            VALUES(:Atividade,:Periodo,:Mes,:DataPrevista,:Hora,:Duracao,:Destinatarios,:Organizador,:Orcamento,:Obs,:idProjeto,
                                                    :idEscola,:idProfessor,:idAnoEscolar, NOW());");
                    $stmt->bindparam(":Atividade", $novaAtividade);
                    $stmt->bindparam(":Periodo", $periodo);
                    $stmt->bindparam(":Mes", $previsto);
                    $stmt->bindparam(":DataPrevista", $previsao);
                    $stmt->bindparam(":Hora", $hora);
                    $stmt->bindparam(":Duracao", $duracao);
                    $stmt->bindparam(":Destinatarios", $destinatarios);
                    $stmt->bindparam(":Organizador", $organizador);
                    $stmt->bindparam(":Orcamento", $orcamento);
                    $stmt->bindparam(":Obs", $observacoes);
                    $stmt->bindparam(":idProjeto", $id_projeto);
                    $stmt->bindparam(":idEscola", $id_escola);
                    $stmt->bindparam(":idProfessor", $id_professor);
                    $stmt->bindparam(":idAnoEscolar", $id_ano);
                    $dominio = explode(',', $dominios);
                    $result = $stmt->execute();
                    $idAtividade = $this->db->lastInsertId();
                    foreach ($dominio as $idDominios) {
                        $stmt->bindParam(':idDominios', $idDominios, PDO::PARAM_INT);
                        $this->setTabelaAtividadesDominios($idAtividade, $idDominios);
                    }
                    $dpt = $this->getUserDepartamentoById($id_professor);
                    $this->setTabelaDepartamentosAtividades($dpt, $idAtividade);
                    $escola = $this->getEscolaById($id_escola);
                    $professor = $this->getNomeById($id_professor);
                    $nome_dominios = $this->getNomeDominios($idAtividade);
                    $nome_projeto = $this->getNomeProjetos($id_projeto);
                    if ($nome_projeto == '') {
                        $nome_projeto = "Sem Projeto/Clube associado";
                    }
                    $this->AtividadeProjetoPdf($idAtividade, $escola, $nome_projeto, $novaAtividade, $periodo, $previsao, $previsto, $nome_dominios, $destinatarios, $organizador, $orcamento, $observacoes, $professor);
                    if (!$result) {
                        print_r($stmt->errorInfo());
                        return array('status' => 'error', 'message' => 'Not done...');
                    } else {
                        return array('status' => 'success', 'message' => 'Record done...');
                    }

                } catch (PDOException $ex) {
                    echo $ex->getMessage();
                }
            }
        }
        else{
            return array('status' => 'error', 'message' => 'OK ');
            }
        

and this is executed one time only and should be 3 times since my array has 3 values..

 

Thanks

Link to comment
Share on other sites

The only "OK" I see in your code is at the end.

Your code is effectively

        if($getDisponibilidadeData ==0){
            // ...
            // your database code
            // ...
        }
        else{
            return array('status' => 'error', 'message' => 'OK ');
            }

BTW, you should prepare the query and bind parameters before the loop. Inside the loop you set any changed variable values and execute. EG
 

prepare;

bind params;

foreach (($idEscola as $id_escola) {

    execute;
}

Try running without the inner foreach (ie just the insert). It may be failing in any of those other methods you are calling.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.