D
D
Danya Kozlovskiy2020-09-25 16:05:24
MySQL
Danya Kozlovskiy, 2020-09-25 16:05:24

How to get json via post in slim and add to database?

Hello, you need to get a post request in json format and write it to the database. But in the end, in $id, when outputting, either null or nothing. Help... I'm
using SLIM 3.
I send this request via postman:
5f6dea411b258669653931.jpeg

My attempt to pull this off:

$app->post('/products', function (\Slim\Http\Request $request, \Slim\Http\Response $response) use ($app) {
    global $mysql;
    header("Content-Type: application/json");
    $id = $request->getParsedBody();
    print_r($id);
   

    $query = "INSERT INTO tete(du, name, price) VALUES ($id[du]},$id[name]},$id[price])";
    if ($result = mysqli_query($mysql, $query)) {
        echo "Все круто";
    } else {
        echo "Все плохо";
    }
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danya Kozlovskiy, 2020-09-26
@morto

The problem was that it was not correctly passing json. There shouldn't be a comma after the last line in json...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question