2017-08-26 122 views
1

我已经jQuery的产生多个表单字段,但只有最后一个记录保存到数据库如何使用laravel 5.4

<tr> 
    <td><input class="form-control" id="quantity[]" name="quantity[]" placeholder="Quantity" type="text"></td> 
    <td><input class="form-control" id="price[]" name="price[]" placeholder="Enter Pice" type="text"></td> 
</tr> 

产生额外的表单字段jQuery的多个表单字段保存到数据库是这里

$('#priceTable').append(
    "<tr> <td><input class='form-control' id='quantity[1]' name=\"quantity[]\" placeholder='Quantity' type='text'></td><td><input class='form-control' name=\"price[]\" id='price[1]' +' placeholder='Enter Pice' type='text'></td> </tr>"\ 
); 

从PHP页面上的结果低于

array([quantity] => Array ([0] => 45 [1] => 60) [price] => Array ([0] => 45000 [1] => 60000) 

即假设环秀将其发布到数据库中,但只发布一个数据库

for ($i=0; $i <= $noQuantity; $i++) 
     { 
      if(!empty($price)){ 
       $data = [ 
        'price' => $priceRe[$i], 
        'quantity' => $quantity[$i] 
       ]; 
       $price->create($data); 
       return redirect('/admin123/category'); 
      } 

     } 

回答

0

这只是一个创造,因为你在循环内重定向,因此它创建的第一个,然后向用户发送到其他页面。尝试将它:

for ($i=0; $i <= $noQuantity; $i++) 
{ 
    if(!empty($price)){ 
     $data = [ 
      'price' => $priceRe[$i], 
      'quantity' => $quantity[$i] 
     ]; 
     $price->create($data); 
    } 
} 
return redirect('/admin123/category'); 
+0

它工作得很好,但得到**(1/1)ErrorException未定义抵消:已发布到数据库 – codePhree

+0

这是环路上的一个错误**后3。你从0到'$ noQuantity',我假设它是数组的长度。所以,你要超过最大指数。这样做:'for($ i = 0; $ i <= $ noQuantity - 1; $ i ++)'或'for($ i = 0; $ i = $ noQuantity - 1; $ i ++)' – ishegg

+0

很高兴能帮到您! :) – ishegg