如何在SQLi中添加多行?

时间:2017-01-18 15:03:12

标签: javascript php

我创建了表格表格,我们可以在其中一个接一个地添加多个行 我想在提交按钮上插入所有行的数据。我只能添加单行,即多行中的顶行

这是我的HTML代码:

<div class = "table-responsive">
      <table class="table table-responsive table-striped table-bordered table-condensed ">
   <thead>
     <tr>
       <th class="text-center">#</th>
       <th>Name Of DE</th>
       <th>Game Plan Posted</th>
       <th>Patch Name </th>
       <th>Summary</th>
       <th>P.O.B</th>
       <th>Dr. Conversion </th>
       <th>Remarks </th>
     </tr>
   </thead>
   <tbody id="dataTable">
     <tr>
       <td><input type="checkbox" name="chk[]" /></td> 
        <td><input type = "text" name = "de[]"></td>
       <td>
        <select id="gamePlan" name="gamePlan[]">
            <option>----- Select ------</option>
            <option>Yes</option>
            <option>No</option>
          </select>
       </td>
       <td><input type = "text" name = "patch[]"></td>
       <td>
        <select id="summary" name="summary[]">
            <option>----- Select ------</option>
            <option>Yes</option>
            <option>No</option>
          </select>
       </td>
        <td><textarea type="textarea" name= "pob[]"></textarea></td>
         <td><input type = "text" name = "drc[]"></td>
         <td><input type = "text" name = "rem[]"></td>

    </tr>

   </tbody>
 </table>
 </div>

这是我的PHP代码:

<?php
  if(isset($_POST['submit'])){
foreach($_POST['de'] as $k => $name) {
    $na =$db->escape( $_POST['de'][$k]);
    $pa =$db->escape( $_POST['patch'][$k]);
    $pob = $db->escape($_POST['pob'][$k]);
    $dr = $db->escape($_POST['drc'][$k]);
    $re = $db->escape($_POST['rem'][$k]);
    $game = $db->escape( $_POST['gamePlan'][$k]);
    $sum = $db->escape($_POST['summary'][$k]);

    // coqueryntinue insertion
   $query = "INSERT INTO misrep(DE, game_plan, patch_name, summary, pob, dr_conversion, remark)
           VALUES('$na', '$game', '$pa', '$sum','$pob','$dr', '$re')";

      if($db->query($query)){
   $session->msg('s',"Product added ");
   redirect('tbl_report.php', false);
 } else {
   $session->msg('d',' Sorry failed to added!');
   redirect('product.php', false);
 }

  }

}

帮助我解决它!感谢

2 个答案:

答案 0 :(得分:0)

改变它:

if(isset($_POST['submit'])){
  foreach($_POST['de'] as $k => $name) {
    $na =$db->escape( $_POST['de'][$k]);
    //paste rest of your bind code  here
    $values [] = "('$na', '$game', '$pa', '$sum','$pob','$dr', '$re')";
  }
  $query = "INSERT INTO misrep(DE, game_plan, patch_name, summary, pob, dr_conversion, remark)
       VALUES " .implode(', ',$values);
  if($db->query($query)){
    $session->msg('s',"Products added ");
    redirect('tbl_report.php', false);
  } else {
    $session->msg('d',' Sorry failed to added!');
    redirect('product.php', false);
  }
}

如果您想在一个查询中插入所有产品。

答案 1 :(得分:0)

@您的常识感谢除了重定向之外的其余代码是正确的。我删除了重定向方法。现在它工作得很完美 感谢