Answer the question
In order to leave comments, you need to log in
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:
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
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 questionAsk a Question
731 491 924 answers to any question