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

I have table which is dynamically generated. I am getting all correct data. I want to store html data to database table. So how do I store it? following is the code for html table

  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>";
  }

On form submit I have done this..

 $data['cart']=array(
        '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'),
        );
        print_r($data);

    $i=0;
        foreach($data['cart'] as $row){
            $product_id=$row['product_id'];
            $product_name=$row['product_name'];
            $packing=$row['packing'];
            $quantity=$row['quantity'];
            $rate=$row['rate'];
            $amount=$row['amount'];
            $query = $this->db->query("insert into 
           phppos_billing_items(product_id,product_name,packing,quantity,rate,amount) values
 ('$product_id','$product_name','$packing','$quantity','$rate','$amount')");
            $i++;
        }

But it displaying only last record in table..Anybody has any idea about this?? I want total table records to save in another table.

$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]")");


}

But it will gives error when no data found for any data So please validate all fields before this code

The trick here is to use the [] notation in your HTML form elements.

So instead of this:

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

You have:

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

If you have specific keys you can do:

<input type='text' name='someVar[myKey1]'/>

In your case I would do this (HTML generation):

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>";
}

Notice the [] after every input name? And this would be the code that responds to the form submission:

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')");
}

http://php.net/manual/en/faq.html.php#faq.html.arrays