News:

AbanteCart v1.4.4 is released.

Main Menu
support

how to save to database via api

Started by shera85, March 15, 2019, 11:13:46 PM

Previous topic - Next topic

shera85

Hi,

I want to extend api to insert into db but for some reason it's returning empty 200 message all the time. Even I'm trying to pass static response it's still giving 200 with empty response.

class ControllerApiAccountDriverLocationCreate extends AControllerAPI
{
    public $data;

    public function post()
    {
       
        $this->extensions->hk_InitData($this, __FUNCTION__);
       
        $request_data = $this->rest->getRequestParams();

        $order_id = $request_data["order_id"];
        $driver_id = $request_data["driver_id"];
        $lat = $request_data["latitude"];
        $long = $request_data["longitude"];

        if ($this->customer->isLoggedWithToken($request['token'])) {
            $this->rest->setResponseData(array('error' => 'Already Logged in. Can not create new account.'));
            $this->rest->sendResponse(401);
            return null;
        }

        $this->loadModel('account/customer');
        $this->loadLanguage('account/driverlocation');
             
        $result = $this->model_account_customer->saveDriverLocation($order_id, $driver_id, $lat, $long);
        $this->data["result"] = $result;

       
        $this->extensions->hk_UpdateData($this, __FUNCTION__);

        $this->rest->setResponseData($this->data);
        $this->rest->sendResponse(200);
    }

}


and model is

   public function saveDriverLocation($order_id, $driver_id, $lat, $long){


        $this->db->query("INSERT INTO `abki_driver_location`
                    SET order_id = '".(int)$order_id."',
                        driver_id = '".(int)$driver_id."',
                        latitude = '".$lat."',
                        longitude = '".$long."',
                        created_date = NOW()");

        return $this->db->getLastId();

    }


What am I doing wrong here

Thanks in advance.

abolabo

hmm.. Your code looks fine..try to debug (with logging $this->log->write(''dddd') or something like that..)
Be sure you sends post-request with token.

appsted

It is not difficult to save database from API, If you have faces any type of issue then you can Hire web developers and solve your all query.

Pulsometros2020

Hi
Maybe it should be verified that the record I want to insert exists or is not empty.
I think a good tutorial shouldn't be a problem.
I suppose that hiring a freelance web developer could solve the problem.
regards ;)

MarkOrion

An API is not a database. It is an access point to an app that can access a database. In this post, we will focus on these specific kinds of APIs — web based APIs that return data in response to a request made by a client. They allow us to go get data from outside sources.

Canadaonlineta

You can reply naturally like this:

> Saving data to a database through an API usually involves sending the data from your application to a backend endpoint. The API then validates the request and uses a database query or ORM to insert the data.
>
> For example, a POST request can send JSON data such as a name, email, or other fields. The backend receives it, validates the values, and saves them securely to the database before returning a success response.
>
> It's also important to use authentication, input validation, and parameterized queries to keep the API and database secure.

Forum Rules Code of conduct
AbanteCart.com 2010 -