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条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题