0

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
    i haven't used CI in a long time, but usually, when you use to start a transaction you end it too, or commit – Kevin Aug 05 '19 at 07:36
  • PD https://stackoverflow.com/questions/29318749/codeigniter-active-record-insert-if-new-or-update-on-duplicate – splash58 Aug 05 '19 at 07:37
  • and why do you have both insert and update on the same method? shouldn't you just check the existence first, then decide whether to use an insert or update, and return a true or false based on insert id or affected rows – Kevin Aug 05 '19 at 07:41
  • After inserting value you should return value as "return 1;" or inserted row's id "return $this->db->insert_id()" – Ashish Singhal Aug 05 '19 at 07:48
  • @AshishSinghal i return success but i got the same respond – Alfred J. Wooten Aug 05 '19 at 08:08
  • Log the success message and check if success is returning proper value. before that just set "return 1" and then check – Ashish Singhal Aug 05 '19 at 09:21

2 Answers2

0

Try these changes in your code

In your controller,

$customer      =   $this->Customer->save_holding_stock($holding_stock_data);
$save_status    =   $this->db->affected_rows();
if ($save_status>0) {
                $returnArr['status'] = '1';
                $returnArr['response'] =  $customer;

            } else {
                $returnArr['response'] = 'No items found';
            }

In your model,

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

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

}
deavapriya
  • 67
  • 10
0

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);
}
Abdallah Abdillah
  • 1,189
  • 13
  • 26