PHP MySql PDO 多次插入不起作用
Posted
技术标签:
【中文标题】PHP MySql PDO 多次插入不起作用【英文标题】:PHP MySql PDO Multiple insert doesn't work 【发布时间】:2015-09-14 08:13:40 【问题描述】:我有这个用于多次插入查询的代码(我必须将数据从数据库传输到另一个数据库并进行一些更新,所以我想使用一个可以自动完成所有这些操作的代码)
$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++;
但是这段代码不起作用。我也尝试将try-catch(PDOException)
用于$sth->execute()
,但它没有显示任何内容。
为什么?
谁说“这个问题是重复的”并没有真正理解这个问题。事实上错误是一个错误的字符:$array[$count]['pr'] = $pfferta['prezzo']
将是$array[$count]['pr'] = $offerta['prezzo']
所以我在另一个问题中找不到答案。
【问题讨论】:
你的代码中$dbh = newPdo2();
和$dbh = newPdo();
是相同还是不同???
不同,因为它们在 2 个不同的数据库中
您没有错误处理。添加一些,您可能会回答自己的问题
例如?选择查询有效。 Insert 是问题所在,但 try-catch 没有显示任何错误。
关于@RiggsFolly,请考虑以下方法来查找您的查询中的错误:***.com/a/11305187/998096(该线程中有更多关于此问题的替代答案)
【参考方案1】:
尝试添加一些简单的检查以确保事情实际上是这样工作的
$res = $sth->execute();
if ( ! $res )
echo sprintf('ERROR: %d - %s', $sth->errorCode(), $sth->errorInfo() );
【讨论】:
以上是关于PHP MySql PDO 多次插入不起作用的主要内容,如果未能解决你的问题,请参考以下文章