doutui2016 2019-08-05 07:33
浏览 162
已采纳

如何在codeigniter上更新或插入新数据

I'm setting up a rest-API on my server, and I want to update a table (i.e "comp_holding_stock"). but every time I test to post new data it returns "No item found"

Here is my controller

   public function create_comp_holding_stock(){

   $returnArr['status'] = '0';
   $returnArr['response'] = '';

   try {
       if (!$this->input->post()) {
           $returnArr['response'] = "Only POST method is allowed";
       } else {

           $holding_stock_data = array(
               'comp_id' => $this->input->post('comp_id'),
               'customer_id' => $this->input->post('customer_id'),
               'quantity' => $this->input->post('quantity'),
               'date' => date('Y-m-d H:i:s')
           );




           if (!isset($holding_stock_data)) {
            $returnArr['response'] = "Some Parameters are missing";
        } else {

            $customer = $this->Customer->save_holding_stock($holding_stock_data);

            if (!$customer) {
                $returnArr['response'] = 'No items found';
            } else {
                $returnArr['status'] = '1';
                $returnArr['response'] =  $customer;
            }
        }
    }
} catch (Exception $ex) {
    $returnArr['response'] = "Error in connection";
    $returnArr['error'] = $ex->getMessage();
}
$response = json_encode($returnArr, JSON_PRETTY_PRINT);
echo $response;

}

And here is my model below

  public function save_holding_stock($holding_stock_data)
{
    // $this->db->trans_start();

    $success = $this->db->insert('comp_holding_stock', $holding_stock_data);

 return $success;;
}

what am i doing wrong? what is the best approach to this scenarios

  • 写回答

2条回答 默认 最新

  • donglian3055 2019-08-05 12:58
    关注

    I would recommend try to check if you have load model in your controller. And in your model try to do this.

    public function save_holding_stock($holding_stock_data, $comp_id=FALSE)
    {
    
        if(!$comp_id == -1 || !$this->exists($comp_id))
        {
            if($this->db->insert('comp_holding_stock', $holding_stock_data))
            {
                $holding_stock_data['comp_id'] = $this->db->insert_id();
    
                return TRUE;
            }
    
            return FALSE;
        }
    
        $this->db->where('comp_id', $comp_id);
    
        return $this->db->update('comp_holding_stock', $holding_stock_data);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏