dougua3705 2015-06-27 12:56
浏览 142

PHP MySql PDO多次插入不起作用

I have this code for a multiple insert query (I have to transfer data from db to another and makes some update, so I wanna use a code that could do all this 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've also tried to use try-catch(PDOException) for $sth->execute() but it doesn't show me anything.

Why?

Who says "this question is a duplicated" doesn't read really the question. Infact the error was a wrong character: $array[$count]['pr'] = $pfferta['prezzo'] would be been $array[$count]['pr'] = $offerta['prezzo']so I couldn't find an answer in another question.

  • 写回答

1条回答 默认 最新

  • douludi8413 2015-06-27 13:31
    关注

    Try adding some simple checks that things actually worked like this

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。