javascript - 使用for循环将多条记录插入数据库

标签 javascript php html mysql

我有一个插入多条记录的表单。

表格有一个:字段用户名(A),BTR(B),使用的设施(C),样本数量(D),以及与不同样本相关的记录(1,2,3..)。

使用此表单,我将多条记录(ABCD1、ABCD2、ABCD3、ABCD4...)插入到数据库中。

我尝试使用 for 循环,但它保存的是最后一条记录值。我做不到,请帮助我。

 if(isset($_POST['add']))
    {
    $user=$_POST['user'];
    $btr=$_POST['btr'];
    $facility=$_POST['facility'];
    $type=$_POST['type'];
    $samplelocation=$_POST['samplelocation'];
    $remarks=$_POST['remarks'];
    $samplecount=$_POST['samplecount'];
    $sql="INSERT INTO  tblfacility(user,btr,facility,type,samplelocation,remarks) VALUES(:user,:btr,:facility,:type,:samplelocation,:remarks)";
    $query = $dbh->prepare($sql);
    $query->bindParam(':user',$user,PDO::PARAM_STR);
    $query->bindParam(':btr',$btr,PDO::PARAM_STR);
    $query->bindParam(':facility',$facility,PDO::PARAM_STR);
    $query->bindParam(':type',$type,PDO::PARAM_STR);
    $query->bindParam(':samplelocation',$samplelocation,PDO::PARAM_STR);
    $query->bindParam(':remarks',$remarks,PDO::PARAM_STR);
    $query->execute();
    $lastInsertId = $dbh->lastInsertId();
    if($lastInsertId)
    {
    $_SESSION['msg']="Sample Listed successfully";
    header('location:manage-books.php');
    }
    else 
    {
    $_SESSION['error']="Something went wrong. Please try again";
    header('location:manage-books.php');
    <label>BTR No.<span style="color:red;">*</span></label>
    <input class="form-control" type="text" name="btr"  required="required" autocomplete="off" />
    </div>

    <label>BTR No.<span style="color:red;">*</span></label>
    <input class="form-control" type="text" name="btr"  required="required" autocomplete="off" />
    </div>

    <div class="form-group">
    <label>Facility Used<span style="color:red;">*</span></label>
    <select class="form-control" type="text" name="facility"  required="required" autocomplete="off"  />
      <option value="AMS">AMS 14C</option>
      <option value="AMS">AMS 10Be</option>
      </select>  
    <p class="help-block"></p>
    </div>
    <div class="form-group">
    <label>Number Of Sample<span style="color:red;">*</span></label>
    <input class="form-control" type="text" name="samplecount" onchange="samplecount()"  required="required" autocomplete="off" />
    </div>
    <td>

    <table   class="table table-striped small-text" id="tb" >
    <tr class="tr-header">
    <h4 style="color:darkGreen;" ><b><u>SAMPLE INFORMATION</u></b></h4> 
    <th class= "col-md-1" align="centre">Sl.No.</th>
    <th class= "col-md-2" align="centre">TYPE OF SAMPLE</th>
    <th  class= "col-md-2" align="centre">Sample Location</th>
    <th class= "col-md-6" align="centre"> Remarks</th>
    <th><a href="javascript:void(0);" style="font-size:18px;" id="addMore" title="Add More Sample"><span class="glyphicon glyphicon-plus"></span></a></th>

    <tr>
    <td><input type="text"  name="slno" value= "<?php echo $i; ?>" class="form-control" ></td> 
    <td><select  type="text" name="type" class="form-control">
      <option value="SELECT TYPE">SELECT TYPE</option>
       <option value="BONE">BONE</option>
       <option value="CARBONATE">CARBONATE</option>
      </select></td>

    <td><input type="text" name="samplelocation" class="form-control" ></td>
    <td><input type="text" name="remarks" class="form-control"></td>

    <td><a href='javascript:void(0);'  class='remove'><span class='glyphicon glyphicon-remove'></span></a></td>
    <?php }?>
    </tr>
    </table>
    <button type="submit" name="add" class="btn btn-info"  align="middle" >ADD </button>
    </form>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> 
    <script>
    $(function(){
        $('#addMore').on('click', function() {
                  var data = $("#tb tr:eq(1)").clone(true).appendTo("#tb");
                  data.find("input").val('');

         });
         $(document).on('click', '.remove', function() {
             var trIndex = $(this).closest("tr").index();
                if(trIndex>1) {
                 $(this).closest("tr").remove();
               } else {
                 alert("Sorry!! Can't remove first row!");
               }
          });
    }); 

    </script>
                                </div>
                            </div>
                                </div>

            </div>

        </div>
        </div>
    </body>
    </html>

最佳答案

在所有输入名称中添加 [],例如:name="user[]"。这将在您提交后为您提供一组输入元素。

所以在你的 if(isset($_POST['add'])) 中你可以得到这个数组 $arr_user = $_POST['user'];然后使用 for 循环循环这些数据。 你的 tr 标签可能是这样的,

<tr class="tr-header">
<h4 style="color:darkGreen;" ><b><u>SAMPLE INFORMATION</u></b></h4> 
<th class= "col-md-1" align="centre">Sl.No.</th>
<th class= "col-md-2" align="centre">TYPE OF SAMPLE</th>
<th  class= "col-md-2" align="centre">Sample Location</th>
<th class= "col-md-6" align="centre"> Remarks</th>
<th><a href="javascript:void(0);" style="font-size:18px;" id="addMore" title="Add More Sample"><span class="glyphicon glyphicon-plus"></span></a></th>

<tr>
<td><input type="text"  name="slno[]" value= "<?php echo $i; ?>" class="form-control" ></td> 
<td><select  type="text" name="type[]" class="form-control">
  <option value="SELECT TYPE">SELECT TYPE</option>
   <option value="BONE">BONE</option>
   <option value="CARBONATE">CARBONATE</option>
  </select></td>

<td><input type="text" name="samplelocation[]" class="form-control" ></td>
<td><input type="text" name="remarks[]" class="form-control"></td>

<td><a href='javascript:void(0);'  class='remove'><span class='glyphicon glyphicon-remove'></span></a></td>
<?php }?>
</tr>

关于javascript - 使用for循环将多条记录插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58061431/

相关文章:

php - 在查询结束时不使用 XDEBUG_SESSION_START 开始调试

php - 使用 php 将图像的 dpi 从 72 更改为 96

javascript - 如何在 HTML 选择中实现重命名选项?

html - 使 div 在悬停时影响另一个 div 的问题

javascript - Recaptcha 弹出警告

javascript - 如何在数组中搜索多个可能的匹配项

javascript - 如何在 Angular JS 中的嵌套 ul li ul li 列表项中重复数据?

javascript - 如何使用jquery在数组中搜索指定值

javascript - 在Ajax中,我想要有2个及更多的输入功能。我怎样才能做到这一点?

javascript - 以 Angular 动态地将元素添加到DOM