• <tfoot id='gH970'></tfoot>

      <legend id='gH970'><style id='gH970'><dir id='gH970'><q id='gH970'></q></dir></style></legend>

    1. <small id='gH970'></small><noframes id='gH970'>

        • <bdo id='gH970'></bdo><ul id='gH970'></ul>

        <i id='gH970'><tr id='gH970'><dt id='gH970'><q id='gH970'><span id='gH970'><b id='gH970'><form id='gH970'><ins id='gH970'></ins><ul id='gH970'></ul><sub id='gH970'></sub></form><legend id='gH970'></legend><bdo id='gH970'><pre id='gH970'><center id='gH970'></center></pre></bdo></b><th id='gH970'></th></span></q></dt></tr></i><div id='gH970'><tfoot id='gH970'></tfoot><dl id='gH970'><fieldset id='gH970'></fieldset></dl></div>
      1. 使用 MySQLi 从 HTML 表单向数据库插入多行

        时间:2023-07-30
          <bdo id='tNltE'></bdo><ul id='tNltE'></ul>

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

                <tbody id='tNltE'></tbody>

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

            2. <tfoot id='tNltE'></tfoot>
              • <legend id='tNltE'><style id='tNltE'><dir id='tNltE'><q id='tNltE'></q></dir></style></legend>

                1. 本文介绍了使用 MySQLi 从 HTML 表单向数据库插入多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试制作一个使用数组的表单,因此一旦它被提交和处理,多行就会插入到我的数据库中.我的主程序比下面更复杂,但我无法让它工作,所以我决定创建一个简单的小程序来更好地理解基本语法,然后将这些技术应用于主程序.我已经使用折旧的 MySQL 让它工作了,但是将它转换为 MySQLi 会导致问题,我想知道我是否可以获得帮助.

                  I'm trying to make a form that uses arrays so once it is submitted and processed multiple rows get inserted into my database. My main program is more complex than below but I could not get it working so I decides to create a small simple program to understand the basic syntax better then apply the techniques to the main program. I have got it to work using the depreciated MySQL but converting it to MySQLi is causing problems that I wonder if I can get help with.

                  我的表单是这样设置的

                  <html>
                  <title>multi row insert test form</title>
                  <body>
                  <table>
                  <form action="process2.php" method="post">
                  <tr>
                  <th>forename</th>
                  <th>surname</th>
                  <th>level</th>
                  </tr>
                  <tr>
                  <td><input type="text" name="fname[]"></td>
                  <td><input type="text" name="sname[]"></td>
                  <td> 
                  <select name="level[]">
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                  </select> 
                  </td>
                  </tr>
                  <tr>
                  <td><input type="text" name="fname[]"></td>
                  <td><input type="text" name="sname[]"></td>
                  <td> 
                  <select name="level[]">
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                  </select> 
                  </td>
                  </tr>
                  <tr>
                  <td><input type="submit" name="submit" value="Submit"></td>
                  </tr>
                  </form>
                  </table>
                  </body>
                  </html>
                  

                  使用MySQLi更新数据库的php页面如下

                  and the php page that updates the database using MySQLi is as below

                  <?php
                  include 'dbconnect2.php';
                  $fname = $_POST['fname'];
                  $sname = $_POST['sname'];
                  $level = $_POST['level'];
                  
                  
                  if ($stmt = $mysqli->prepare("INSERT INTO people (fname, sname, level) values (?, ?, ?)")) {
                  
                  $stmt->bind_param('ssi', $fname, $sname, $level);
                  
                  for ($i=0; $i<2; $i++)
                  {
                  $fname[$i] = $fname;
                  $sname[$i] = $sname;
                  $level[$i] = $level;
                  
                  $stmt->execute();
                  echo "Done";
                  }
                  
                  $stmt->close();
                  }
                  ?>
                  

                  推荐答案

                  或者,减少重写现有代码的次数:

                  Or, with less rewriting your existing code:

                  $fnames = $_POST['fname'];
                  $snames = $_POST['sname'];
                  $levels = $_POST['level'];
                  
                  $stmt = $mysqli->prepare("INSERT INTO people (fname, sname, level) values (?, ?, ?)")
                  
                  for ($i=0; $i<count($fnames); $i++) {
                      $fname = $fnames[$i];
                      $sname = $snames[$i];
                      $level = $levels[$i];
                      $stmt->bind_param('ssi', $fname, $sname, $level);
                  
                      $stmt->execute();
                  }
                  echo "Done";
                  
                  $stmt->close();
                  

                  这篇关于使用 MySQLi 从 HTML 表单向数据库插入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mysqli 准备语句从数组动态构建 INSERT 查询 下一篇:PHP mysqli bind_param 文本类型

                  相关文章

                  最新文章

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

                    1. <legend id='mesAQ'><style id='mesAQ'><dir id='mesAQ'><q id='mesAQ'></q></dir></style></legend>
                    2. <small id='mesAQ'></small><noframes id='mesAQ'>

                        <bdo id='mesAQ'></bdo><ul id='mesAQ'></ul>

                    3. <tfoot id='mesAQ'></tfoot>