PHP MySql PDO Multiple insert not working

I have this code for a multiple insert query (I need to transfer data from db to another and make some updates, so I want to use code that could do it all automatically)

$query = "select * from pubblicate order by idPubblicate asc";

$dbh = newPdo2();
$dbh->exec("set names utf8");

$sth = $dbh->prepare($query);
$sth->execute();

$count = 0;
$query2 = "insert into published_offer 
            (codice_onshop,nome,inbreve,anteprima,
             galleria1,galleria2,galleria3,galleria4,prezzo,
             tp_prezzo,bonus_usabile,proposta,condizioni,
             prenotare,categoria,description,keywords,
             valido_da,valido_a) ";

while($offerta = $sth->fetch(PDO::FETCH_ASSOC)) {
    $array[$count]['id'] = $offerta['idPubblicate'];
    $array[$count]['co'] = $offerta['codiceOfferta'];
    $array[$count]['no'] = $offerta['nomeOfferta'];
    $array[$count]['ib'] = $offerta['inBreve'];
    $array[$count]['ke'] = $offerta['keywords'];
    $array[$count]['de'] = $offerta['description'];
    $array[$count]['pr'] = $pfferta['prezzo'];
    $array[$count]['pe'] = $offerta['persona'];
    $array[$count]['da'] = $offerta['daTimer'];
    $array[$count]['a']  = $offerta['aTimer'];
    $array[$count]['an'] = $offerta['anteprima'];
    $array[$count]['g1'] = $offerta['galleria1'];
    $array[$count]['g2'] = $offerta['galleria2'];
    $array[$count]['g3'] = $offerta['galleria3'];
    $array[$count]['g4'] = $offerta['galleria4'];
    $array[$count]['pro'] = $offerta['proposta'];
    $array[$count]['con'] = $offerta['condizioni'];
    $array[$count]['pre'] = $offerta['prenotare'];
    $array[$count]['bo'] = 999;

    if($offerta['italia']=="Sì")      $array[$count]['ca'] = "ita";
    else if($offerta['europa']=="Sì") $array[$count]['ca'] = "eur";
    else if($offerta['mondo']=="Sì")  $array[$count]['ca'] = "mon";
    $count++;
}

$query2 .= "values (:co,:no,:ib,:an,:g1,:g2,
                    :g3,:g4,:pr,:pe,:bo,:pro,:con,
                    :pre,:ca,:de,:ke,:da,:a)";

$dbh = newPdo(); 
$dbh->exec("set names utf8");
$sth = $dbh->prepare($query2);

$i=0;
echo $array[0]['no'] . " " . count($array) . " " . $array[125]['no'] . "<br>" . $query2 . "<br>";

while($i<count($array)) {
    $sth->bindParam(":co", $array[$i]['co']);
    $sth->bindParam(":no", $array[$i]['no']);
    $sth->bindParam(":ib", $array[$i]['ib']);
    $sth->bindParam(":an", $array[$i]['an']);
    $sth->bindParam(":g1", $array[$i]['g1']);
    $sth->bindParam(":g2", $array[$i]['g2']);
    $sth->bindParam(":g3", $array[$i]['g3']);
    $sth->bindParam(":g4", $array[$i]['g4']);
    $sth->bindParam(":pr", $array[$i]['pr']);
    $sth->bindParam(":pe", $array[$i]['pe']);
    $sth->bindParam(":bo", $array[$i]['bo']);
    $sth->bindParam(":pro",$array[$i]['pro']);
    $sth->bindParam(":con",$array[$i]['con']);
    $sth->bindParam(":pre",$array[$i]['pre']);
    $sth->bindParam(":ca", $array[$i]['ca']);
    $sth->bindParam(":de", $array[$i]['de']);
    $sth->bindParam(":ke", $array[$i]['ke']);
    $sth->bindParam(":da", $array[$i]['da']);
    $sth->bindParam(":a",  $array[$i]['a'] );

    $sth->execute();
    $i++;
}

      

But this code doesn't work. I also tried using try-catch(PDOException)

for $sth->execute()

, but it doesn't show me anything.

Why?

Whoever said that "this question is duplicated" does not actually read the question. Infact the error was the wrong symbol: $array[$count]['pr'] = $pfferta['prezzo']

would $array[$count]['pr'] = $offerta['prezzo']

, so I couldn't find the answer in another question.

+3


source to share


1 answer


Try adding some simple checks that really worked for this



$res = $sth->execute();
if ( ! $res ) {
    echo sprintf('ERROR: %d - %s', $sth->errorCode(), $sth->errorInfo() );
}

      

-1


source







All Articles