我使用表输入一些字段并将其存储在db中。如果用户想要存储更多数据,我使用“添加新”按钮来添加新行,但我希望在表中使用自己的主键分别存储每一行。
下面是我的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>发布于 2017-05-02 04:07:58
可以在数据库表中创建id列。您需要将其设置为主键和自动增量(如果使用mysql,则在phpmyadmin中设置AI选项)。您只需存储其他数据,id列将自动接受其值,这对于每一行都是唯一的。
https://stackoverflow.com/questions/43729860
复制相似问题