如何使用php,mysql和jQuery将每个行的主键分别存储在数据库中

时间:2017-05-02 03:56:30

标签: php jquery mysql html-table row

  

我正在使用table输入一些字段并将其存储在db中。我用过Add   如果用户想要存储更多数据,则添加新行的新按钮,但我想要   将每一行与其自己的主键分别存储在表中。

下面是我的html代码表,添加了新按钮,jQuery用于添加新按钮:

<div class="table-responsive">
  <table id="datatable-buttons"  class="table table-striped jambo_table bulk_action">
    <thead>
      <tr class="headings">
        <th>
          <input type="checkbox" id="check-all" class="flat">
        </th>
        <th class="column-title">Account</th>
        <th class="column-title">Debits</th>
        <th class="column-title">Credits</th>
        <th class="column-title">Description</th> 
        <th class="column-title">Name</th>                          
        <th class="column-title no-link last"><span class="nobr">Action</span>
        </th>
        <th class="bulk-actions" colspan="7">
          <a class="antoo" style="color:#fff; font-weight:500;">Bulk Actions ( <span class="action-cnt"> </span> ) <i class="fa fa-chevron-down"></i></a>
        </th>
      </tr>
    </thead>                                
    <tbody>
      <tr class="even pointer">
        <td class="a-center ">
          <input type="checkbox" class="flat" name="table_records">
        </td>                                    
        <td class=" "><select class="form-control" name="journalname">
          <option>Choose option</option>                   
          <?php
          $sql = mysqli_query($conn,'SELECT * FROM `chartofaccounts`');
          while ($row1 = mysqli_fetch_array($sql)) 
            {?>
              <option value="<?php echo $row1["name"];?>"><?php echo $row1["name"];?></option>
          <?php  }?>
        </select></td>
        <td class=""><input class="form-control" type="text" name="debit" ></td>
        <td class=" "><input class="form-control" type="text" name="credit"></td>
        <td class=" "><input class="form-control" type="text" name="descc"></td> 
        <td class=" "><input class="form-control" type="text" name="name"></td>        
        <td class=" last"><a href="#">View</a>
        </td>
      </tr>                           
    </tbody>                           
  </table>
  <button type="button" id="add">Add New</button>
</div>

jQuery添加新行:

<script type="text/javascript">
      $("#add, .check ").click(function() {
      $('#datatable-buttons tbody>tr:last')
      .clone(true)
      .insertAfter('#datatable-buttons 
      tbody>tr:last').find('input').each(function(){
      $(this).val('');
      });
    });
</script>

1 个答案:

答案 0 :(得分:0)

您可以在数据库表中创建一个id列。您需要将其设置为主键和自动增量(如果您使用的是mysql,则在phpmyadmin中设置AI选项)。您只需要存储其他数据,id列将自动获取其值,该值对于每一行都是唯一的。