Mysql:表多行插入一个sql行

时间:2017-05-29 09:13:46

标签: php mysql codeigniter

目前我正在创建一个软件,我可以在表格中创建多行。 Purchase Table

但是为什么我编写PHP代码,它会在SQL中创建多行,但我需要针对一个发票号创建一行。

Html(使用ajax自动生成):

<tr>
                            <td><input class="form-control" type="text" name="product_name[]" value="'.$proData['name'].'"></td>
                            <td><input class="form-control qnt'.$proData['id'].'" min="0" step="any" type="number" name="qnt[]" placeholder="Quantity"></td>
                            <td><input class="form-control unit_price'.$proData['id'].'" type="number" min="0" step="any" name="unit_price[]" placeholder="Unit Price"></td>
                            <td><input class="form-control pack_size'.$proData['id'].'" type="number" min="0" step="any" name="pack_size[]" placeholder="Pack Size"></td>
                            <td><input class="form-control unit_pack'.$proData['id'].'" type="number" min="0" step="any" name="unit_pack[]" placeholder="Unit Pack (jar/Drum)"></td>
                            <td><input class="form-control total_kg'.$proData['id'].'" type="number" name="total_kg[]" placeholder="Total Kg/s" value="" readonly></td>
                            <td><input class="form-control total_price'.$proData['id'].'" type="number" name="total_price[]" placeholder="Total Price &#2547;" value="" readonly></td>
                            <td><button type="button" class="rowDelete btn btn-danger"><i class="icon-trash"></i></button></td>
                        </tr>

php代码:

public function add(){
        // Form Inputs
        $invoice = $this->input->post('invoice');
        $date = $this->input->post('date');
        $created_by = $this->input->post('created_by');
        $products = $this->input->post('products');
        $qnt = $this->input->post('qnt[]');
        $unit_price = $this->input->post('unit_price[]');
        $pack_size = $this->input->post('pack_size[]');
        $unit_pack = $this->input->post('unit_pack[]');
        $total_kg = $this->input->post('total_kg[]');
        $total_price = $this->input->post('total_price[]');
        $payed = $this->input->post('payed');
        $price_less = $this->input->post('price_less');
        $price_discount = $this->input->post('price_discount');
        $price_due = $this->input->post('price_due');
        $grand_total = $this->input->post('grand_total');
        $payMethod = $this->input->post('payMethod');
        $bank_name = $this->input->post('bank_name');
        $cheque_no = $this->input->post('cheque_no');
        $bank_acc_no = $this->input->post('bank_acc_no');

        for($i = 0; $i < count($qnt); $i++){
            $purchaseData = array(
                'invoice_no' => $invoice,
                'date' => $date,
                'product_id' => $products,
                'create_date' => $created_by,
                'qnt' => $qnt[$i],
                'unit_price' => $unit_price,
                'pack_size' => $pack_size,
                'unit_pack' => $unit_pack,
                'total_kg' => $total_kg,
                'total_price' => $total_price,
                'payed' => $payed,
                'price_less' => $price_less,
                'price_discount' => $price_discount,
                'price_due' => $price_due,
                'grand_total' => $payMethod,
                'payMethod' => $grand_total,
                'bank_name' => $bank_name,
                'cheque_no' => $cheque_no,
                'bank_acc_no' => $bank_acc_no
            );

            $PurchaseQuery = $this->db->insert('purchase', $purchaseData);

            if($PurchaseQuery){
                $purchaseAdded = "Product Purchase Add.";
                $this->session->set_flashdata('purchaseAdded', $purchaseAdded);
                redirect('Purchase');
            }
        }
    }

请帮助。我正在使用CI。

2 个答案:

答案 0 :(得分:2)

要求我退出不同。你能做的是什么?而不是循环项目。我的意思是

count($qnt)

只是json_encode()多个项目

或更改您的模式并规范您的表格 将订单和订单项目放在2个不同的表格中

答案 1 :(得分:1)

请这样做。

public function add(){
        // Form Inputs
        $invoice = $this->input->post('invoice');
        $date = $this->input->post('date');
        $created_by = $this->input->post('created_by');
        $products = $this->input->post('products');
        $qnt = $this->input->post('qnt[]');
        $unit_price = $this->input->post('unit_price[]');
        $pack_size = $this->input->post('pack_size[]');
        $unit_pack = $this->input->post('unit_pack[]');
        $total_kg = $this->input->post('total_kg[]');
        $total_price = $this->input->post('total_price[]');
        $payed = $this->input->post('payed');
        $price_less = $this->input->post('price_less');
        $price_discount = $this->input->post('price_discount');
        $price_due = $this->input->post('price_due');
        $grand_total = $this->input->post('grand_total');
        $payMethod = $this->input->post('payMethod');
        $bank_name = $this->input->post('bank_name');
        $cheque_no = $this->input->post('cheque_no');
        $bank_acc_no = $this->input->post('bank_acc_no');

            $purchaseData = array(
                'invoice_no' => $invoice,
                'date' => $date,
                'product_id' => $products,
                'create_date' => $created_by,
                'qnt' => $qnt[$i],
                'unit_price' => $unit_price,
                'pack_size' => $pack_size,
                'unit_pack' => $unit_pack,
                'total_kg' => $total_kg,
                'total_price' => $total_price,
                'payed' => $payed,
                'price_less' => $price_less,
                'price_discount' => $price_discount,
                'price_due' => $price_due,
                'grand_total' => $payMethod,
                'payMethod' => $grand_total,
                'bank_name' => $bank_name,
                'cheque_no' => $cheque_no,
                'bank_acc_no' => $bank_acc_no
            );

            $PurchaseQuery = $this->db->insert('purchase', $purchaseData);

            if($PurchaseQuery){
                $purchaseAdded = "Product Purchase Add.";
                $this->session->set_flashdata('purchaseAdded', $purchaseAdded);
                redirect('Purchase');
            }
    }

让我知道结果。