Slim Framework: Insert query issue

1k views Asked by At

I am new to the Slim Framework. I don't know how to perform an insert query in Slim framework.

Please can somebody show me an example or tutorial to understand it better. I have read the Android hive tutorial but am still not clear.

I am not able to post the params when using advanced REST API.

0 Response is generated.

Below is my code for index.php file:

global $name;

$app->get('/saveEvent', function() {
    global $user_id;
    $response = array();
    $db = new DbHandler();
    $name = $app->request->post('name');
    $result = $db->createUser($name);

    //$response["error"] = false;
    if ($result != NULL) {
        $response["error"] = false;
        $response["message"] = "Task created successfully";
    } else {
        $response["error"] = true;
        $response["message"] = "Failed to create task. Please try again";
    }
    echoRespnse(201, $response);
});

$app->run();
1

There are 1 answers

0
Vinita Pawar On

Here is an example of how you would perform a post:

$app->post("/book", function () use($app, $db) {
    $app->response()->header("Content-Type", "application/json");
    $book = $app->request()->post();
    $result = $db->books->insert($book);
    echo json_encode(array("id" => $result["id"]));
});