代码之家  ›  专栏  ›  技术社区  ›  snookian

PDO插入foreach

  •  0
  • snookian  · 技术社区  · 6 年前

    我正在尝试将一个值数组多次插入到一个表中。

    我有一个简单的数组,它是由用户选中一个框生成的,这就是添加到数组中的内容,然后我想将每个值插入到一个表中,我想我可以用foreach循环和迭代$I来完成它,但是看起来我不能,我不需要担心安全性或其他任何问题,因为这是由两个人内部使用的。

    foreach($detailsinvoice as $desc){ 
      $conn3 = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
      $sql3 = "INSERT INTO 
                 xero_invoices (ContactName, Description)
                 VALUES (:ContactName, :Description)";  
      $st3 = $conn3->prepare ( $sql3 );
      $st3->bindValue( ":ContactName", $this->ContactName, PDO::PARAM_STR );
      $st3->bindValue( ":Description", $desc, PDO::PARAM_STR );
      $st3->execute();
      $this->InvoiceNumber = $conn3->lastInsertId();
      $conn3 = null;
    }
    

    这是我的第一次尝试,但我发现连接只能使用一次,然后退出,所以我尝试了一次迭代,但我再次了解到,用PDO语句不能这样做。

    $i = 3;
    foreach($detailsinvoice as $desc){ 
      $conn[$i] = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
      $sql[$i] = "INSERT INTO 
                 xero_invoices (ContactName, Description)
                 VALUES (:ContactName, :Description)";  
      $st[$i] = $conn[$i]->prepare ( $sql[$i] );
      $st[$i]->bindValue( ":ContactName", $this->ContactName, PDO::PARAM_STR );
      $st[$i]->bindValue( ":Description", $desc, PDO::PARAM_STR );
      $st[$i]->execute();
      $this->InvoiceNumber = $conn[$i]->lastInsertId();
      $conn[$i] = null;
      $i++;
    }
    

    detailsinvoice 是数组,而ContactName每次都是相同的(ContactName工作只需要找出数组的循环)

    2 回复  |  直到 6 年前
        1
  •  3
  •   u_mulder    6 年前

    准备好的陈述的特点是你可以 一个声明 一旦

    // Create a connection
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "INSERT INTO 
            xero_invoices (ContactName, Description)
            VALUES (:ContactName, :Description)";  
    // Create a statement
    $st = $conn->prepare ($sql);
    foreach ($detailsinvoice as $desc) { 
        // bind values and execute statement in a loop:
        $st->bindValue( ":ContactName", $this->ContactName, PDO::PARAM_STR );
        $st->bindValue( ":Description", $desc, PDO::PARAM_STR );
        $st->execute();
        $this->InvoiceNumber = $conn->lastInsertId();
    }
    // this is optional
    $conn = null;
    
        2
  •  0
  •   RiggsFolly    6 年前

    我不知道你是从哪里想到的连接只能使用一次。在脚本中只能连接一次。那么只要你把东西放在商店里 $conn

    // connect ONCE per script
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    
    // write the query once
    $sql = "INSERT INTO xero_invoices (ContactName, Description)
                 VALUES (:ContactName, :Description)";  
    
    // and prepare it once.
    $st = $conn->prepare ( $sql );
    
    // now loop over the array of parameters any number of times you like
    foreach($detailsinvoice as $desc){ 
    
        $st->bindValue( ":ContactName", $this->ContactName, PDO::PARAM_STR );
        $st->bindValue( ":Description", $desc, PDO::PARAM_STR );
        $st->execute();
    
        // this line looks wrong, as $this->InvoiceNumber will get overwritten
        // each time round the loop
        //$this->InvoiceNumber = $conn->lastInsertId();
    
        // maybe you ment this, so at least you would have them all???? 
        $this->InvoiceNumber[] = $conn->lastInsertId();
    
        // or I have to assume you are going to add another query HERE
        // that will use that ID
    }
    

    预处理语句的概念是,它被传递到数据库,编译、优化并保存在数据库中,就像存储过程一样。

    一旦准备好就可以反复使用。您所做的只是在每次执行时将新值放入参数中。

    推荐文章