我无法使用Codeigniter for REST API将多行插入mysql
我已经创建了我无法使用Codeigniter for REST API将多行插入mysql,codeigniter,Codeigniter,我已经创建了Api,用于使用codeigniter框架将多行插入数据库。当我试图使用Postman将数据插入数据库时,因为数据将仅通过Postman发送,所以值不会存储到数据库中 我已经参考了很多论坛,但我没有找到解决我的问题的办法。 请帮我找到解决办法 这是我的代码:mycantorler.php public function addProducts_post(){ $product_name = $this->post('product_name');
Api
,用于使用codeigniter
框架将多行插入数据库。当我试图使用Postman
将数据插入数据库时,因为数据将仅通过Postman发送,所以值不会存储到数据库中
我已经参考了很多论坛,但我没有找到解决我的问题的办法。
请帮我找到解决办法
这是我的代码:mycantorler.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);
}
}
下面是新的更新代码:mycantorller.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);
}
}
}
public function addProducts\u post(){
$product_id=array();
$userData=array();
$totalProducts=$this->post('total_items');
$userData['grand_total']=$this->post('total');
$userData['user\u id']=$this->post('user\u 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');
//打印($userData);死亡;
//验证post数据
如果(!empty($userData['grand_total'])和(&!empty($userData['user_id'])){
$insert=$this->user->insertOrder($userData);
//打印($insert);模具;
如果($插入){
对于($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);
}
//打印($ordItemData);模具;
//$data=$this->user->insertitems($ordItemData);
如果($数据){
$this->response([
“状态”=>TRUE,
'消息'=>'感谢您成功订购。'
],REST_控制器::HTTP_OK);
//设置响应并退出
}否则{
//设置响应并退出
//错误的_请求(400)是HTTP响应代码
$this->response(“未添加到联系人中。”,REST\u Controller::HTTP\u BAD\u REQUEST);
}
}否则{
//设置响应并退出
$this->response(“填充所有数据”,REST\u控制器::HTTP\u BAD\u请求);
}
}
}
是的,我已将$data添加到insert_批('tableName',$data)中。但是在邮递员上作为DB query builder获取错误。请显示错误您得到了什么?在我的错误文件名:C:/xampp/htdocs/IMS_API/system/Database/DB_query_builder.php行号:1498时发生了一个数据库错误,而此代码可能解决了这个问题,如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
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);
}
}
}