首页 > 解决方案 > 我无法使用 Codeigniter for REST API 将多行插入 mysql

问题描述

我创建了用于使用框架Api将多行插入数据库。codeigniter当我尝试将数据插入数据库时Postman​​,数据将仅通过邮递员发送,值不会存储到数据库中。

我已经提到了很多论坛,但我没有找到解决我的问题的方法。请帮助我获得解决方案。

这是我的代码:MyContorller.php

public function addProducts_post(){

        $product_name = $this->post('product_name');
        $quantity_per_pack = $this->post('quantity_per_pack');

        $product_per_pack_unit = $this->post('product_per_pack_unit');

        $data = array();

         for ($i = 0; $i < count($this->post('product_name')); $i++)
        {
        $data[] = array(
            'product_name' => $product_name[$i],
            'quantity_per_pack' => $quantity_per_pack[$i],
            'product_per_pack_unit' => $product_per_pack_unit[$i],
        );
        }
        $insert =  $this->product->add($data);
        if($insert){
     $this->response([
                'status' => TRUE,
                'message' => 'Products has been added successfully.'
     ], REST_Controller::HTTP_OK);
     }
    else {
        //set the response and exit
        $this->response([
            'status' => FALSE,
            'message' => 'Not added'
        ], REST_Controller::HTTP_NOT_FOUND);
    }
    }

My_Model.php

public function add($data = array()) {

    $insert = $this->db->insert_batch('product', $data);
    if($insert){
        return $this->db->insert_id();
    }else{
        return false;
    }

}   

标签: codeigniter

解决方案


这是新的更新代码:MyContorller.php

public function addProducts_post(){

    $product_id = array();
    $userData = array();
    $totalProducts = $this->post('total_items');
    $userData['grand_total'] = $this->post('total');
    $userData['user_id'] = $this->post('user_id');
    $product_id[] = $this->post('product_id_1');
    $product_id[] = $this->post('product_id_2');
    $product_id[] = $this->post('product_id_3');
    $product_id[] = $this->post('product_id_4');
    $product_id[] = $this->post('product_id_5');
    $quantity[] = $this->post('quantity_1');
    $quantity[] = $this->post('quantity_2');
    $quantity[] = $this->post('quantity_3');
    $quantity[] = $this->post('quantity_4');
    $quantity[] = $this->post('quantity_5');
    //  $userData['customer_id']  = $this->post('customer_id');
    //print_r($userData);die;
    // Validate the post data
    if(!empty($userData['grand_total']) && !empty($userData['user_id']) ){
        $insert = $this->user->insertOrder($userData);
        // print_r($insert);die;
        if($insert){
            for ($i = 0; $i < $totalProducts ; $i++)
            {
                $ordItemData['order_id']    = $insert;
                $ordItemData['user_id']     = $userData['user_id'];
                $ordItemData['product_id']  = $product_id[$i];
                $ordItemData['quantity']    = $quantity[$i];
                $ordItemData['sub_total']   = $userData['grand_total'];

               $data= $this->user->insertitems($ordItemData);
            }
            // print_r($ordItemData);die;
            //$data = $this->user->insertitems($ordItemData);
            if($data){
                $this->response([
                    'status' => TRUE,
                    'message' => 'Thank Succefully order.'

                ], REST_Controller::HTTP_OK); 


                // Set the response and exit

            }else{
                // Set the response and exit
                //BAD_REQUEST (400) being the HTTP response code
                $this->response("Not Added in Contact.", REST_Controller::HTTP_BAD_REQUEST);
            }
        }else{
            // Set the response and exit
            $this->response("Fill All Data.", REST_Controller::HTTP_BAD_REQUEST);
        }

    }

}

推荐阅读