Insert data in db with form using slim

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

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

1 Like

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]);
});
1 Like