dongxian3574 2015-01-14 10:19
浏览 71

将关联数组存储到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.

  • 写回答

2条回答 默认 最新

  • douchenhui5569 2015-01-16 05:32
    关注
    $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

    评论

报告相同问题?