我创建了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;
}
}
答案 0 :(得分:0)
这是新的更新代码: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);
}
}
}