# Insert data in db in POST method gives error code 0 Slim v4

**URL:** <https://discourse.slimframework.com/t/insert-data-in-db-in-post-method-gives-error-code-0-slim-v4/5808>\
**Category:** Questions\
**Created:** [February 4, 2024, 12:05pm UTC](https://discourse.slimframework.com/t/insert-data-in-db-in-post-method-gives-error-code-0-slim-v4/5808 "2024-02-04T12:05:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pablo12](https://avatars.discourse-cdn.com/v4/letter/p/cdc98d/32.png) [@Pablo12](https://discourse.slimframework.com/u/Pablo12)\
**Post date:** [February 4, 2024, 12:05pm UTC](https://discourse.slimframework.com/t/insert-data-in-db-in-post-method-gives-error-code-0-slim-v4/5808/1 "2024-02-04T12:05:07Z")

</div>

Hello,I’m new to this framework and I´m following a some years old Angular course and can’t make this POST method work. While building an Slim 4 API REST I need to insert json data in my db from a form I simulate on Postman.

```auto
$app->post('/products', function (Request $request, Response $response,$db): Response {
    $data = $request->getParsedBody();
    
    $html = var_export($data, true);
    $response->getBody()->write($html);
    $data2 = implode($data);
    $decode = json_decode($data2, true);
    //display json arrays content
    var_dump($decode);

 $query = "INSERT INTO productos VALUES (NULL,".
            "'{$decode['nombre']}',".
            "'{$decode['description']}',".
            "'{$decode['precio']}',".
            "NULL".
            ");";
var_dump($query);
$insert = $db->query($query);

 if($insert) {
        $result = array(
            'status' => 'success',
            'code' => 200,
            'message' => 'Product created'
        );
    }
    $resultado = implode( $result);
    $decode2 = json_decode($resultado, true);
    var_dump($decode2);

    return $response;
});

```

The $db variable contains a db connection:

```auto
$db= new mysqli('localhost', 'root','','db_name');

```

When executed the POST method with I receive **Call to a member function query() on array** error code 0 in line 87." $insert = $db-\>query($query);"

 ![Captura de pantalla 2024-02-04 130038](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/d/d55f17fcef9948d13745a018e6c3e94191888ab8.png)

The insert query looks like it’s correct but maybe this “query()” method is deprecated?  
I have tried different methods from the official documentation but didn’t fixed it yet.

Thank you.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [February 4, 2024, 6:32pm UTC](https://discourse.slimframework.com/t/insert-data-in-db-in-post-method-gives-error-code-0-slim-v4/5808/2 "2024-02-04T18:32:01Z")

</div>

> [@Pablo12](#):
>
> `app->post('/products', function (Request $request, Response $response,$db): Response {`

Have you tried to pass the $db variable like this?

```php
$app->post('/products', function (Request $request, Response $response) use ($db): Response {
     // ...
}

```
