Php 商店<;td>;元素导入数据库

Php 商店<;td>;元素导入数据库,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有一个动态创建的表。意味着头是固定的,行数据将在某个ajax调用后动态追加。我的问题是如何在数据库中保存带有product_id(即id)的数量值 <tr> <th></th> <th>Product Name</th> <th>Unit Price</th> <th>Quantity</th>

我有一个动态创建的表。意味着头是固定的,行数据将在某个ajax调用后动态追加。我的问题是如何在数据库中保存带有product_id(即
id)的数量值

 <tr> 
   <th></th>                   
   <th>Product Name</th>
   <th>Unit Price</th>
   <th>Quantity</th>                   
   <th>Total </th>
 </tr>

品名
单价
量
全部的
//这一部分将在我使用自动完成搜索的ajax调用之后追加

 <tr id="+ui.item.id+">   // product_id
    <td><i class='flaticon-delete-1 delete-row' onclick='deleteRow(this)'></i></td>
     <td>"+ui.item.value+"</td>
     <td>"+ui.item.unit_price+"</td>
     <td><input type='text' class='quantity' value='1'></td> //quantity
     <td class='total'>"+ui.item.unit_price+"</td>
  </tr>
//产品标识
“+ui.item.value+”
“+ui.项目.单价+”
//数量
“+ui.项目.单价+”

以数组形式存储数量和产品:

     <td><input type='text' name="id[]" class='productclass' value='+ui.item.id+'></td>
     <td><input type='text' name="quantity[]" class='quantity' value='1'></td>

如果不想显示产品id,可以隐藏:

<input type='hidden' name="id[]" class='productclass' value="+ui.item.id+">

在服务器端:

//Check input data
if(in_array("",$_POST['id'], true)){
//Empty id
}else{
  $countid = count($_POST['id']);
  for($j=0;$j<$countid;$j++){
      $id = $_POST['id'][$j];
      $quantity = $_POST['quantity'][$j];
      //code for saving into the database
  }
}
//检查输入数据
if(在数组中(“,$”POST['id'],true)){
//空id
}否则{
$countid=计数($_POST['id']);

对于($j=0;$j请尝试执行此操作。对于此操作,请使用参考id进行选择

这个怎么样(用固定值替换你的ui.item


我无法将id保留为输入类型文本。请参阅更新的代码,如果我为输入类型保留不同的名称,您可以将其放在tr元素之间的某个位置。我如何跟踪哪个产品具有数量?for循环将为您提供每个项目及其数量。尝试执行此操作。为此,请特别选择具有参考id的您对于你的问题,我有三个答案。接受对你有用的答案或留下评论。为你写答案的人这样做是为了获得更多的声誉。
<table>
     <tr class="product-list" id="100">
        <td><i class='flaticon-delete-1 delete-row onclick='deleteRow(this)'></i></td>
         <td>Product 1</td>
         <td>200</td>
         <td><input type="text" class="quantity" value="1"></td>
         <td class='total'>200</td>
      </tr>

      <tr class="product-list" id="101">
        <td><i class='flaticon-delete-1 delete-row onclick='deleteRow(this)'></i></td>
         <td>Product 2</td>
         <td>250</td>
         <td><input type="text" class="quantity" value="1"></td>
         <td class='total'>250</td>
      </tr>
</table>
<button type="button" onclick="postData()">Post</button>
function postData(){
    var products = [];
    $('.product-list').each(function(index, el){

      var productId = $(this).attr('id');
      var quantity = $(this).find('.quantity').val();
      var total = $(this).find('.total').text();
      products[index]['id'] = productId;
      products[index]['quantity'] = quantity;
      products[index]['total'] = total;
    });

  //now post $products through AJAX


}