0

我在一个 pdo 查询中插入多行时遇到了一些麻烦。我使用我制作的这段代码。但是当我在数据库中查看插入时,自动增量 id 会上升,但缺少一些插入。只有第一个插入留在数据库中。

id  idcom   couleurb    couleurf    taille
169 160 blanc       grisfonc    Tableau L
170 161 blanc       grisfonce   Tableau L
172 162 blanc       grisfonce   Tableau L   

<?php    
foreach ($panier->getContenu() as $produit) {
    $queryTableau = $bdd->prepare('INSERT INTO tableaux (idcommande, colorfront, colorback, size, police, mots) VALUES (:id, :cf, :cb, :si, :po, :mo)');
    $queryTableau->bindParam(':id', $id);
    $queryTableau->bindParam(':cf', $cf);
    $queryTableau->bindParam(':cb', $cb);
    $queryTableau->bindParam(':si', $si);
    $queryTableau->bindParam(':po', $po);
    $queryTableau->bindParam(':mo', $mo);

    $description = $produit->getDescription();
    $id = $_SESSION['commande']['id'];
    $cf = $description['couleurFront'];
    $cb = $description['couleurBack'];
    $si = $produit->getNameProduit();
    $po = $description['police'];
    $mo = $description['mots'];

    $queryTableau->execute();
}
?>
4

2 回答 2

0

根据http://php.net/manual/en/pdostatement.execute.php一些驱动程序需要在执行下一条语句之前关闭光标。

$queryTableau->execute();
$queryTableau->closeCursor();
于 2013-04-02T17:39:14.340 回答
0

不要在每次 foreach 迭代中进行准备。

<?php    

$queryTableau = $bdd->prepare('INSERT INTO tableaux (idcommande, colorfront, colorback, size, police, mots) VALUES (:id, :cf, :cb, :si, :po, :mo)');

foreach ($panier->getContenu() as $produit) {

    $queryTableau->bindParam(':id', $id);
    $queryTableau->bindParam(':cf', $cf);
    $queryTableau->bindParam(':cb', $cb);
    $queryTableau->bindParam(':si', $si);
    $queryTableau->bindParam(':po', $po);
    $queryTableau->bindParam(':mo', $mo);

    $description = $produit->getDescription();
    $id = $_SESSION['commande']['id'];
    $cf = $description['couleurFront'];
    $cb = $description['couleurBack'];
    $si = $produit->getNameProduit();
    $po = $description['police'];
    $mo = $description['mots'];
   $queryTableau->execute();
 }
?> 

看看将您的数据与准备/执行分开是否可以解决您的一些问题。

于 2013-04-02T17:15:46.450 回答