• <bdo id='Fzp7G'></bdo><ul id='Fzp7G'></ul>
      1. <small id='Fzp7G'></small><noframes id='Fzp7G'>

        <tfoot id='Fzp7G'></tfoot>

        <legend id='Fzp7G'><style id='Fzp7G'><dir id='Fzp7G'><q id='Fzp7G'></q></dir></style></legend>
        <i id='Fzp7G'><tr id='Fzp7G'><dt id='Fzp7G'><q id='Fzp7G'><span id='Fzp7G'><b id='Fzp7G'><form id='Fzp7G'><ins id='Fzp7G'></ins><ul id='Fzp7G'></ul><sub id='Fzp7G'></sub></form><legend id='Fzp7G'></legend><bdo id='Fzp7G'><pre id='Fzp7G'><center id='Fzp7G'></center></pre></bdo></b><th id='Fzp7G'></th></span></q></dt></tr></i><div id='Fzp7G'><tfoot id='Fzp7G'></tfoot><dl id='Fzp7G'><fieldset id='Fzp7G'></fieldset></dl></div>

        PHP PDO 使用循环插入

        时间:2023-10-05

            <tbody id='UZjeB'></tbody>
            1. <tfoot id='UZjeB'></tfoot>

              <small id='UZjeB'></small><noframes id='UZjeB'>

              • <i id='UZjeB'><tr id='UZjeB'><dt id='UZjeB'><q id='UZjeB'><span id='UZjeB'><b id='UZjeB'><form id='UZjeB'><ins id='UZjeB'></ins><ul id='UZjeB'></ul><sub id='UZjeB'></sub></form><legend id='UZjeB'></legend><bdo id='UZjeB'><pre id='UZjeB'><center id='UZjeB'></center></pre></bdo></b><th id='UZjeB'></th></span></q></dt></tr></i><div id='UZjeB'><tfoot id='UZjeB'></tfoot><dl id='UZjeB'><fieldset id='UZjeB'></fieldset></dl></div>
                  <bdo id='UZjeB'></bdo><ul id='UZjeB'></ul>
                  <legend id='UZjeB'><style id='UZjeB'><dir id='UZjeB'><q id='UZjeB'></q></dir></style></legend>
                  本文介绍了PHP PDO 使用循环插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在使用 PDO 将多条记录插入数据库时​​遇到问题.我可以成功添加一条记录,但是一旦我添加了 foreach 循环,它就会失败.在阅读了一些关于此的其他 SO 问题后,我相信我需要绑定"我的变量,尽管我对正确的语法感到完全困惑.

                  I am having trouble using PDO to insert multiple records into a database. I can successfully add a single record, but as soon as I add the foreach loop, it fails. After reading a number of other SO questions regarding this, I believe I need to "bind" my variables, although I am completely confused on the proper syntax.

                  这是我创建的原始函数:

                  Here is the original function I created:

                  <? function addToDatabase () {
                      //Get All Variables
                      $timestamp = date("Y-m-d H:i:s");
                      $schoolName = $_SESSION['schoolName'];
                      $schoolStreet = $_SESSION['schoolStreet'];
                      $schoolCity = $_SESSION['schoolCity'];
                      $schoolState = $_SESSION['schoolState'];
                      $schoolZip = $_SESSION['schoolZip'];
                      $schoolContactName = $_SESSION['schoolContactName'];
                      $schoolContactTitle = $_SESSION['schoolContactTitle'];
                      $schoolContactPhone = $_SESSION['schoolContactPhone'];
                      $schoolCsontactEmail = $_SESSION['schoolContactEmail'];
                      $inputMethod = $_SESSION['inputMethod'];
                  
                      $studentDataArray = $_SESSION['studentDataArray'];
                  
                      $studentFirstNameField = $_SESSION['studentFirstNameField'];
                      $studentLastNameField = $_SESSION['studentLastNameField'];
                      $studentStreetField = $_SESSION['studentStreetField'];
                      $studentCityField = $_SESSION['studentCityField'];
                      $studentStateField = $_SESSION['studentStateField'];
                      $studentZipcodeField = $_SESSION['studentZipcodeField'];
                      $studentDOBField = $_SESSION['studentDOBField'];
                      $studentGenderField = $_SESSION['studentGenderField'];
                      $studentGradeField = $_SESSION['studentGradeField'];
                  
                      //Connnect to Database
                      $host = 'myHost';
                      $un = 'myUsername';
                      $pw = 'myPassword';
                      $db_name = 'myTable';
                  
                      try {
                          $conn = new PDO("mysql:host=$host;dbname=$dbName", $un, $pw);
                          echo 'Connected to database<br>';
                  
                          $sql = "INSERT INTO studentData (originallyAddedOn, inputMethod, studentFirst, studentLast, studentStreet, studentCity, studentState, studentZip, studentDOB, studentGender, studentGrade, schoolName, schoolStreet, schoolCity, schoolState, schoolZip, schoolContactName, schoolContactTitle, schoolContactEmail, schoolContactPhone) VALUES (:originallyAddedOn, :inputMethod, :studentFirst, :studentLast, :studentStreet, :studentCity, :studentState, :studentZip, :studentDOB, :studentGender, :studentGrade, :schoolName, :schoolStreet, :schoolCity, :schoolState, :schoolZip, :schoolContactName, :schoolContactTitle, :schoolContactEmail, :schoolContactPhone)";
                  
                          foreach ($studentDataArray as $student){
                              $q = $conn->prepare($sql);
                              echo $student[$studentFirstNameField]."<br>";
                              $q->execute(array(  ':originallyAddedOn'=>$timestamp,
                                              ':inputMethod'=>$inputMethod,
                                              ':studentFirst'=>$student[$studentFirstNameField],
                                              ':studentLast'=>$student[$studentLastNameField],
                                              ':studentStreet'=>$student[$studentStreetField],
                                              ':studentCity'=>$student[$studentCityField],
                                              ':studentState'=>$student[$studentStateField],
                                              ':studentZip'=>$student[$studentZipField],
                                              ':studentDOB'=>$student[$studentDOBField],
                                              ':studentGender'=>$student[$studentGenderField],
                                              ':studentGrade'=>$student[$studentGradeField],
                                              ':schoolName'=>$schoolName,
                                              ':schoolStreet'=>$schoolStreet,
                                              ':schoolCity'=>$schoolCity,
                                              ':schoolState'=>$schoolState,
                                              ':schoolZip'=>$schoolZip,
                                              ':schoolContactName'=>$schoolContactName,
                                              ':schoolContactTitle'=>$schoolContactTitle,
                                              ':schoolContactEmail'=>$schoolContactEmail,
                                              ':schoolContactPhone'=>$schoolContactPhone));           
                              }
                              // close the database connection
                              $dbh = null;
                          }
                          catch(PDOException $e) {
                              echo $e->getMessage();
                              }
                      }
                  

                  $studentDataArray 看起来像这样:

                   0 => //student 1
                      array
                        [0] => 'Joe' //First
                        [1] => 'Smith' //Last
                        [2] => '101 Main St' //Street
                        [3] => 'Boston' //City
                        [4] => 'MA' //State
                        [5] => '01234' //Zip
                        [6] => '2000-01-01' //Date of Birth
                        [7] => 'Male' //Gender
                        [8] => '12'  //Grade
                  
                   1 => //Student 2
                      array
                        [0] => 'Jane'
                        [1] => 'Smith'
                        [2] => '99 Main St'
                        [3] => 'Boston'
                        [4] => 'MA'
                        [5] => '01234'
                        [6] => '2000-02-02'
                        [7] => 'Female'
                        [8] => '10'
                  

                  <小时>更新:对于那些感兴趣的人,这是我修复错误后的最终功能:


                  UPDATE: For those that are interested, here is my final function after I fixed the errors:

                  <? function addToDatabase ($dataArray) {
                  
                      //Connnect to Database
                      $host = 'myHost';
                      $un = 'myUsername';
                      $pw = 'myPassword';
                      $db_name = 'myTable';    
                  
                      try {
                          $conn = new PDO("mysql:host=$host;dbname=$dbName", $un, $pw);
                          echo 'Connected to database<br>';
                  
                          $sql = "INSERT INTO studentData (originallyAddedOn, inputMethod, studentFirst, studentLast, studentStreet, studentCity, studentState, studentZip, studentDOB, studentGender, studentGrade, schoolName, schoolStreet, schoolCity, schoolState, schoolZip, schoolContactName, schoolContactTitle, schoolContactEmail, schoolContactPhone) VALUES (:originallyAddedOn, :inputMethod, :studentFirst, :studentLast, :studentStreet, :studentCity, :studentState, :studentZip, :studentDOB, :studentGender, :studentGrade, :schoolName, :schoolStreet, :schoolCity, :schoolState, :schoolZip, :schoolContactName, :schoolContactTitle, :schoolContactEmail, :schoolContactPhone)";
                          $q = $conn->prepare($sql);
                  
                          foreach ($dataArray as $student){
                              $a = array (':originallyAddedOn'=>$student['timestamp'],
                                          ':inputMethod'=>$student['inputMethod'],
                                          ':studentFirst'=>$student['studentFirst'],
                                          ':studentLast'=>$student['studentLast'],
                                          ':studentStreet'=>$student['studentStreet'],
                                          ':studentCity'=>$student['studentCity'],
                                          ':studentState'=>$student['studentState'],
                                          ':studentZip'=>$student['studentZip'],
                                          ':studentDOB'=>$student['studentDOB'],
                                          ':studentGender'=>$student['studentGender'],
                                          ':studentGrade'=>$student['studentGrade'],
                                          ':schoolName'=>$student['schoolName'],
                                          ':schoolStreet'=>$student['schoolStreet'],
                                          ':schoolCity'=>$student['schoolCity'],
                                          ':schoolState'=>$student['schoolState'],
                                          ':schoolZip'=>$student['schoolZip'],
                                          ':schoolContactName'=>$student['schoolContactName'],
                                          ':schoolContactTitle'=>$student['schoolContactTitle'],
                                          ':schoolContactEmail'=>$student['schoolContactEmail'],
                                          ':schoolContactPhone'=>$student['schoolContactPhone']);
                  
                              if ($q->execute($a)) {          
                                  // Query succeeded.
                                  } else {
                                      // Query failed.
                                      echo $q->errorCode();
                                      }
                              // close the database connection
                              $dbh = null;
                              echo "Insert Complete!";
                          }
                          }
                          catch(PDOException $e) {
                              echo $e->getMessage();
                              }
                      }
                  

                  推荐答案

                  你不需要绑定你的变量.我以前用类似的代码做过这个.很难说到底出了什么问题.你有例外吗 - 如果有,那是什么?

                  You dont need to bind your variables. Ive done this before with similar code. Its hard to say whats going wrong though. Do you get an exception - if so what is it?

                  我认为唯一错误的是你在循环中进行了准备......应该更像是:

                  The only thing i see wrong is you have your preparation inside the loop... should be more like:

                  try {
                           $conn = new PDO("mysql:host=$host;dbname=$dbName", $un, $pw);
                           echo 'Connected to database<br>';
                  
                          $sql = "INSERT INTO studentData (originallyAddedOn, inputMethod, studentFirst, studentLast, studentStreet, studentCity, studentState, studentZip, studentDOB, studentGender, studentGrade, schoolName, schoolStreet, schoolCity, schoolState, schoolZip, schoolContactName, schoolContactTitle, schoolContactEmail, schoolContactPhone) VALUES (:originallyAddedOn, :inputMethod, :studentFirst, :studentLast, :studentStreet, :studentCity, :studentState, :studentZip, :studentDOB, :studentGender, :studentGrade, :schoolName, :schoolStreet, :schoolCity, :schoolState, :schoolZip, :schoolContactName, :schoolContactTitle, :schoolContactEmail, :schoolContactPhone)";
                  
                          // prepare once... exceute many :-)
                          $q = $conn->prepare($sql); 
                  
                          foreach($studentDataArray as $student) {
                              $q->execute($yourDataArray);
                              // do other stuff if needed
                  
                          }
                  
                  } catch(PDOException $e) {
                    echo $e->getMessage();
                  }
                  

                  这篇关于PHP PDO 使用循环插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP PDO 缓冲查询问题 下一篇:为什么 PDO 不允许多个同名占位符?

                  相关文章

                  最新文章

                    <legend id='LmrPf'><style id='LmrPf'><dir id='LmrPf'><q id='LmrPf'></q></dir></style></legend>
                    <i id='LmrPf'><tr id='LmrPf'><dt id='LmrPf'><q id='LmrPf'><span id='LmrPf'><b id='LmrPf'><form id='LmrPf'><ins id='LmrPf'></ins><ul id='LmrPf'></ul><sub id='LmrPf'></sub></form><legend id='LmrPf'></legend><bdo id='LmrPf'><pre id='LmrPf'><center id='LmrPf'></center></pre></bdo></b><th id='LmrPf'></th></span></q></dt></tr></i><div id='LmrPf'><tfoot id='LmrPf'></tfoot><dl id='LmrPf'><fieldset id='LmrPf'></fieldset></dl></div>

                      <small id='LmrPf'></small><noframes id='LmrPf'>

                      • <bdo id='LmrPf'></bdo><ul id='LmrPf'></ul>
                      <tfoot id='LmrPf'></tfoot>