Insert data in db with form using slim


#1

hello there, i’m a php begginer and i was introduced to slim, i think is pretty cool, but, i don’t know how to use it yet, i’m using the code below and it returns me that the method has to be post, but in the way i see, it’s alredy post, thank you

https://pastebin.com/yQzS4N5y


#2

How are you creating the request? And I assume you’re going to sanitize those variables. :wink:


#3

I think this line will not work in Slim:

$app->request->post("/",function(){

Also all SQL queries must be escaped for security reasons.

Here is an example:

use Slim\Http\Request;
use Slim\Http\Response;

$app->post('/', function (Request $request, Response $response) {

    // Get POST data
    $post = (array)$request->getParsedBody();

    $row = [
        'nome' => $post['nome'],
        'email' => $post['email'],
        'senha' => $post['senha']
    ];

    $sql = "INSERT INTO teste SET nome=:nome, email=:email, senha=:senha;";

    /** @var PDO $pdo */
    $pdo = $this->get(PDO::class);
    $success = $pdo->prepare($sql)->execute($row);

    return $response->withJson(['success' => $success]);
});