Php 将关联数组存储到mysql数据库

Php 将关联数组存储到mysql数据库,php,arrays,codeigniter,Php,Arrays,Codeigniter,我有一个动态生成的表。我得到了所有正确的数据。我想将html数据存储到数据库表中。那么我该如何存储它呢?下面是html表的代码 foreach($res->result() as $row ){ echo "<tr>"; echo "<td><input type='hidden' style='width:80%;' value='".$row->product_id."' name='product_id[]

我有一个动态生成的表。我得到了所有正确的数据。我想将html数据存储到数据库表中。那么我该如何存储它呢?下面是html表的代码

  foreach($res->result() as $row ){
  echo "<tr>";
            echo "<td><input type='hidden'  style='width:80%;'  value='".$row->product_id."'
   name='product_id[]'/></td>";
            echo "<td><input type='hidden'  style='width:80%;'  value='".$product_name."'
   name='product_name[]'/></td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td style='width:40%;'>".$product_name."</td>";
            echo "<td><input type='text' style='width:30%;' id='packing' name='packing[]'/></td>";
            echo "<td><input type='text' class='quantity' style='width:80%;' readonly=''
   value='".$row->quantity."' name='quantity[]'/></td>";
            echo "<td><input type='text' name='rate' style='width:80%;' class='rate'
   name='rate[]'/></td>";
            echo "<td><input type='text' style='width:100%;' class='amount' readonly=''
  name='amount[]'/></td>";
            echo "</tr>";
  }
但它只显示了表中的最后一条记录..有人知道这个吗??我希望将总的表记录保存到另一个表中。

$product\U id=$this->input->post('product\U id'),
$product_id =$this->input->post('product_id'),
        $product_name =$this->input->post('product_name'),
        $packing =$this->input->post('packing'),
        $quantity =$this->input->post('quantity'),
        $rate =$this->input->post('rate'),
        $amount =$this->input->post('amount'),

$total = count ($product_id);

for($i=0;$i<$total;$i++){
       $query = $this->db->query("insert into 
           phppos_billing_items(product_id,product_name,packing,quantity,rate,amount) values
 ("$product_id[$i]","$product_name[$i]","$packing[$i]","$quantity[$i]","$rate[$i]","$amount[$i]")");


}
$product\U name=$this->input->post('product\U name'), $packing=$this->input->post('packing'), $quantity=$this->input->post('quantity'), $rate=$this->input->post('rate'), $amount=$this->input->post('amount'), $total=计数($product\U id); 对于($i=0;$idb->query(“插入到 phppos_计费项目(产品id、产品名称、包装、数量、费率、金额)值 (“$product_id[$i]”、“$product_name[$i]”、“$packing[$i]”、“$QUOTE[$i]”、“$rate[$i]”、“$amount[$i]”); }
但是,如果找不到任何数据的数据,则会出现错误,因此请验证此代码之前的所有字段。这里的技巧是在HTML表单元素中使用[]符号

因此,与此相反:

<input type='text' name='someVar'/>

设置会话变量是服务器端的,设置AJAX选项是客户端的。你没有意义。请提供一些代码尝试将名称更改为name='product\u id'改为name='product\u id[]'好的……我会试试……好的……我得到了所有的数据……但是我如何将其存储到数据库……使用insert query into loop??你有这行代码吗:foreach($\u GET['product\U id']作为$index=>$product\U id){
<input type='text' name='someVar'/>
<input type='text' name='someVar[]'/>
<input type='text' name='someVar[myKey1]'/>
foreach($res->result() as $row ){
    echo "<tr>";
    echo "<td><input type='hidden' value='".$row->product_id."' name='product_id[]'/></td>";
    echo "<td><input type='hidden' value='".$product_name."' name='product_name[]'/></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td style='width:40%;'>".$product_name."</td>";
    echo "<td><input type='text' name='packing[]'/></td>";
    echo "<td><input type='text' class='quantity' readonly='' value='".$row->quantity."' name='quantity[]'/></td>";
    echo "<td><input type='text' name='rate' class='rate' name='rate[]'/></td>";
    echo "<td><input type='text' class='amount' readonly='' name='amount[]'/></td>";
    echo "</tr>";
}
foreach ($_GET['product_id'] as $index => $product_id) {
    $product_id = $_GET['product_id'][$index];
    $product_name = $_GET['product_name'][$index];
    $packing = $_GET['packing'][$index];
    $quantity = $_GET['quantity'][$index];
    $rate = $_GET['rate'][$index];
    $amount = $_GET['amount'][$index];
    phppos_billing_items(product_id,product_name,packing,quantity,rate,amount) values ('$product_id','$product_name','$packing','$quantity','$rate','$amount')");
}