首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用php、mysql和jQuery将每行的主键分别存储在数据库中

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

Stack Overflow用户
提问于 2017-05-02 03:56:29
回答 1查看 135关注 0票数 0

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

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

代码语言:javascript
复制
<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以添加新行:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2017-05-02 04:07:58

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43729860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档