gmc1103 Posted September 11, 2018 Share Posted September 11, 2018 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted September 11, 2018 Share Posted September 11, 2018 foreach ($idEscola as $id_escola) { echo $id_escola."<br>"; } // <--- loop ends here The only thing that happens for each id is the echo of its value. The rest is outside the loop and happens once. Quote Link to comment Share on other sites More sharing options...
gmc1103 Posted September 11, 2018 Author Share Posted September 11, 2018 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted September 11, 2018 Share Posted September 11, 2018 Any messages output? Quote Link to comment Share on other sites More sharing options...
gmc1103 Posted September 11, 2018 Author Share Posted September 11, 2018 Yes it says OK, no errors messages But i have only record inserted.... Quote Link to comment Share on other sites More sharing options...
Barand Posted September 11, 2018 Share Posted September 11, 2018 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. Quote Link to comment Share on other sites More sharing options...
gmc1103 Posted September 11, 2018 Author Share Posted September 11, 2018 Hi Barand Done, works now. You are the best Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.