Can I do more than 1 Insert using PDO?

Asked

Viewed 329 times

0

I want to do more than 1 Insert using PDO and a php file, can I? Example:

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type,X-Prototype-Version,X-Requested-With');

date_default_timezone_set('America/Sao_Paulo');

include_once("conPDO.php");

$pdo = conectar();

$data = file_get_contents("php://input");
$data = json_decode($data);

$msg  = $data->msg;
$idUsuario  = $data->idUsuario;
$idCep  = $data->idCep;
$nome  = $data->nome;
$foto ='';

$qryEnd=$pdo->prepare("SELECT * FROM cep WHERE idCep=:idCep");
$qryEnd->bindValue("idCep", $idCep);
$qryEnd->execute();

while ($linha=$qryEnd->fetch(PDO::FETCH_ASSOC)) {
        $idCep  = $linha['idCep'];
        $uf = $linha['uf'];
        $cidade = utf8_encode($linha['cidade']);
        $bairro = utf8_encode($linha['bairro']);
        $logradouro = utf8_encode($linha['logradouro']);
}
$data = date('Y-m-d,H:m:s');

$diaEHora = explode(',', $data);
$data = $diaEHora[0];
$hora = $diaEHora[1];

$insereMsgLogra=$pdo->prepare("INSERT INTO avisosLogradouro (idAvisoLogradouro, idUsuario, estado, cidade, bairro, logradouro, msg, foto, data, hora) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$insereMsgLogra->bindValue(1, NULL); 
$insereMsgLogra->bindValue(2, $idUsuario); 
$insereMsgLogra->bindValue(3, $uf); 
$insereMsgLogra->bindValue(4, $cidade); 
$insereMsgLogra->bindValue(5, $bairro); 
$insereMsgLogra->bindValue(6, $logradouro); 
$insereMsgLogra->bindValue(7, $msg); 
$insereMsgLogra->bindValue(8, $foto);
$insereMsgLogra->bindValue(9, $data);
$insereMsgLogra->bindValue(10, $hora);
$insereMsgLogra->execute();

$insereMsgBairro=$pdo->prepare("INSERT INTO avisosBairro (idAvisoBairro, idUsuario, estado, cidade, bairro, msg, foto, data, hora) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
$insereMsgBairro->bindValue(1, NULL); 
$insereMsgBairro->bindValue(2, $idUsuario); 
$insereMsgBairro->bindValue(3, $uf); 
$insereMsgBairro->bindValue(4, $cidade); 
$insereMsgBairro->bindValue(5, $bairro); 
$insereMsgBairro->bindValue(6, $msg); 
$insereMsgBairro->bindValue(7, $foto);
$insereMsgBairro->bindValue(8, $data);
$insereMsgBairro->bindValue(9, $hora);
$insereMsgBairro->execute() or die(print_r($insereMsgBairro->errorInfo()); 
?>

Is it possible to do that? 'Cause I tried and only the first one worked.

In the php log shows this:

[01-Feb-2016 19:49:09 Europe/Berlin] PHP Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be Removed in a Future version. To avoid this Warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream Instead. in Unknown on line 0

  • Yes it is possible, there are no limits for PDO :D

  • See what is the error of the second Insert, maybe it is violation of some Uniq key field.

  • How I see PDO errors?

  • Makes, $insereMsgBairro->execute() or die(print_r($insereMsgBairro->errorInfo());

  • Beauty @rray, I’ll try

  • Look, the error you gave was this: http://localhost:8888/systems/system_web/Ionic/vcApp/www/php/enviaMsgLogra.php 500 (Internal Server Error) Apparently it’s an error in my php code

  • Yes, puts ini_set('display_errors', true); error_reporting(E_ALL); at the beginning of this file.

  • I already have it in my code

  • Only by the log of the apache there.

  • In the apache log I did not find, but php did. .

  • Put as text that is easier to read, this is the complete code?

  • Sorry, but how so, put as text? No, have other selects to fetch other data, tb

  • The error is like an image, I can’t see.

  • Ahh yes, it’s a print I took... I’ll change it then

  • @rray, put the error message, log, text, post

  • I think I found the mistake...

  • I thought it was a mistake, but that wasn’t the problem

  • 1

    Fixed this: $inserts MsgBairro->execute() or die(print_r($Pdo->errorInfo()); Left to close 1 parenthesis

Show 13 more comments

1 answer

0

Your while is closing before making any Insert.
With this the code block referring to the Insert is executed only once.

 while ($linha=$qryEnd->fetch(PDO::FETCH_ASSOC)) {
/* ---------------------------------------------------------------------- *
    A cada loop o valor das variáveis é atualizado, mas não é utilizado.
 * ---------------------------------------------------------------------- */
     $idCep  = $linha['idCep'];
     $uf = $linha['uf'];
     $cidade = utf8_encode($linha['cidade']);
     $bairro = utf8_encode($linha['bairro']);
     $logradouro = utf8_encode($linha['logradouro']);
 }

/* Seus blocos de insert estão depois do WHILE */

$insereMsgLogra=$pdo->prepare("INSERT INTO avisosLogradouro (idAvisoLogradouro, idUsuario, estado, cidade, bairro, logradouro, msg, foto, data, hora) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$insereMsgLogra->bindValue(1, NULL); 
$insereMsgLogra->bindValue(2, $idUsuario); 
$insereMsgLogra->bindValue(3, $uf); 
$insereMsgLogra->bindValue(4, $cidade); 
...
$insereMsgLogra->execute();

I hope I’ve helped...

Browser other questions tagged

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