Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php pdo多行插入_Php_Mysql_Pdo - Fatal编程技术网

php pdo多行插入

php pdo多行插入,php,mysql,pdo,Php,Mysql,Pdo,在一个pdo查询中插入多行时遇到问题。 我使用我自己编写的代码。 但当我在db中查看插入时,自动增量id会上升,但缺少一些插入。只有第一个插入留在数据库中 id idcom couleurb couleurf taille 169 160 blanc grisfonc Tableau L 170 161 blanc grisfonce Tableau L 172 162 blanc grisfonce Tableau L

在一个pdo查询中插入多行时遇到问题。 我使用我自己编写的代码。 但当我在db中查看插入时,自动增量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();
}
?>
id idcom couleurb couleurf taille
169 160布兰科·格里斯丰表L
170 161布兰科·格里斯丰画面L
172 162布兰克·格里斯丰画面L

不要在每次迭代时都做准备

<?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();
 }
?> 

查看将数据与prepare/execute分离是否可以解决一些问题。

根据某些驱动程序的要求,在执行下一条语句之前关闭游标

$queryTableau->execute();
$queryTableau->closeCursor();

虽然这通常是正确的建议,但它并没有回答我在foreach中尝试binding和queryid的问题,你检查值了吗?var_dump($description)至少?$description很好,我只是检查一下