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

时间:2015-01-14 10:19:44

标签: 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[]'/></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>";
  }

在表单提交上我已经完成了这个..

 $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++;
        }

但它只显示表中的最后一条记录。任何人都对此有任何想法?我希望将总表记录保存在另一个表中。

2 个答案:

答案 0 :(得分:1)

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


}

但是当没有找到任何数据的数据时它会出错。所以请在此代码之前验证所有字段

答案 1 :(得分:0)

这里的技巧是在HTML表单元素中使用[]表示法。

所以不要这样:

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

你有:

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

如果你有特定的钥匙,你可以这样做:

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

在你的情况下,我会这样做(HTML生成):

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

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