Php Mysql:表将多行插入到一个sql行中

Php Mysql:表将多行插入到一个sql行中,php,mysql,codeigniter,Php,Mysql,Codeigniter,目前我正在创建一个软件,可以在表中创建多行 但是为什么我写PHP代码,它会在SQL中创建多行,但是我需要针对一个发票号创建一行 Html(使用ajax自动生成): php代码: public function add(){ // Form Inputs $invoice = $this->input->post('invoice'); $date = $this->input->post('date');

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

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

Html(使用ajax自动生成):


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');
            }
        }
    }
公共函数添加(){
//表格输入
$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\u pack=$this->input->post('unit\u 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\u折扣=$this->input->post('price\u折扣');
$price\u due=$this->input->post('price\u due');
$grand_total=$this->input->post('grand_total');
$payMethod=$this->input->post('payMethod');
$bank_name=$this->input->post('bank_name');
$check\u no=$this->input->post('check\u no');
$bank_acc_no=$this->input->post('bank_acc_no');
对于($i=0;$i$invoice,
“日期”=>$date,
“产品id”=>$products,
“创建日期”=>$created\u by,
“qnt”=>$qnt[$i],
“单价”=>美元单价,
“包装尺寸”=>$pack\U尺寸,
“单位包装”=>美元单位包装,
“总重量”=>$total\u kg,
“总价”=>美元总价,
“已付”=>已付美元,
'price\u less'=>price\u less美元,
“价格折扣”=>美元价格折扣,
“价格到期日”=>$price\u到期日,
“总计”=>$payMethod,
“payMethod”=>总计$grand_,
“银行名称”=>美元银行名称,
“支票编号”=>美元支票编号,
“银行账户号”=>$bank账户号
);
$PurchaseQuery=$this->db->insert('purchase',$purchaseData);
if($PurchaseQuery){
$purchaseAdded=“产品购买添加。”;
$this->session->set_flashdata('purchaseAdded',$purchaseAdded);
重定向(“购买”);
}
}
}
请帮忙。我正在使用CI。

请执行此操作

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

并让我知道结果。

我退出的要求不同。你能做的是什么?而不是在项目之间循环。我是说

count($qnt)
只需json_encode()即可对多个项目进行编码

或者更改您的模式并使您的表正常化

要将订单和订单项放在两个不同的表中

是,因为您使用for循环将数据附加到数组中?qnt必须大于1,因此插入了多行。您想插入多个数据吗?老实说,我还没有理解这些问题。如果您需要多个数据,请使用批量导入。只需将值('123','123'),('123','123')…@Arslan.H插入示例(ex1,ex1)中即可。如果我只插入,我会得到“消息:数组到字符串转换”错误bcoz每次选择产品时,ajax都会自动生成我的表行。我的问题是“我有一个发票号&我需要将所有数据插入一个sql行。我的意思是,如果采购发票相同,则所有产品插入同一行”@Exprator Yes your r r right。这就是我请求帮助的原因。你能清楚地告诉我你想做什么吗?如果您想插入一行,请删除for循环,然后按正常方式插入谢谢您的ans。但我收到错误消息“消息:数组到字符串转换”。我的html输入文件名是相同的“”(表行使用ajax自动生成)。您可以共享您的网站url吗?您好。请共享您的网站url@美美花 谢谢你的帮助#斯瓦纳给了我正确的答案。
count($qnt)