Skip to main content
Commonmark migration
Origen Enlace

Estoy intentando ejecutar el siguiente código en Slim V.4 para borrar un registro en MariaDB y no está sucediendo prácticamente nada.

¿Qué podría ser?

$app->delete('/app/user/delete/{id}', function (Response $response, Request $request, $args){
    $id = $args['id'];
    $sql = "DELETE FROM users WHERE id = '$id'";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute([$id]);
        $db = null;
        echo '{"notice": "Usuario borrado con éxito"}';
    }catch (PDOException $e) {
        echo '{"error": {"text": ' . $e->getMessage() . '}';
    }
});

En la consola de insomnia dice:

INTERNAL SERVER ERROR 500.

Y en la consola Linux:

Fatal error: Uncaught TypeError: Argument 1 passed to {closure}() must implement interface Psr\Http\Message\ResponseInterface, instance of Slim\Psr7\Request given, called in /home/martyn/APP/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php on line 42 and defined in /home/martyn/APP/src/routes/users.php:72

 

Stack trace:

 

#0 /home/martyn/APP/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(42): {closure}(Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array)

 

#1 /home/martyn/APP/vendor/slim/slim/Slim/Routing/Route.php(372): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array)

 

#2 /home/martyn/APP/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): Slim\Routing\Route->handle(Object(Slim\Psr7\Request)) #3 /home/martyn/APP/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))

 

#4 /home/martyn/APP/vendor/slim/slim/Slim/Routing/Route.php(333): Slim\MiddlewareDispatche in /home/martyn/APP/src/routes/users.php on line 72

Estoy intentando ejecutar el siguiente código en Slim V.4 para borrar un registro en MariaDB y no está sucediendo prácticamente nada.

¿Qué podría ser?

$app->delete('/app/user/delete/{id}', function (Response $response, Request $request, $args){
    $id = $args['id'];
    $sql = "DELETE FROM users WHERE id = '$id'";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute([$id]);
        $db = null;
        echo '{"notice": "Usuario borrado con éxito"}';
    }catch (PDOException $e) {
        echo '{"error": {"text": ' . $e->getMessage() . '}';
    }
});

En la consola de insomnia dice:

INTERNAL SERVER ERROR 500.

Y en la consola Linux:

Fatal error: Uncaught TypeError: Argument 1 passed to {closure}() must implement interface Psr\Http\Message\ResponseInterface, instance of Slim\Psr7\Request given, called in /home/martyn/APP/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php on line 42 and defined in /home/martyn/APP/src/routes/users.php:72

 

Stack trace:

 

#0 /home/martyn/APP/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(42): {closure}(Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array)

 

#1 /home/martyn/APP/vendor/slim/slim/Slim/Routing/Route.php(372): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array)

 

#2 /home/martyn/APP/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): Slim\Routing\Route->handle(Object(Slim\Psr7\Request)) #3 /home/martyn/APP/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))

 

#4 /home/martyn/APP/vendor/slim/slim/Slim/Routing/Route.php(333): Slim\MiddlewareDispatche in /home/martyn/APP/src/routes/users.php on line 72

Estoy intentando ejecutar el siguiente código en Slim V.4 para borrar un registro en MariaDB y no está sucediendo prácticamente nada.

¿Qué podría ser?

$app->delete('/app/user/delete/{id}', function (Response $response, Request $request, $args){
    $id = $args['id'];
    $sql = "DELETE FROM users WHERE id = '$id'";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute([$id]);
        $db = null;
        echo '{"notice": "Usuario borrado con éxito"}';
    }catch (PDOException $e) {
        echo '{"error": {"text": ' . $e->getMessage() . '}';
    }
});

En la consola de insomnia dice:

INTERNAL SERVER ERROR 500.

Y en la consola Linux:

Fatal error: Uncaught TypeError: Argument 1 passed to {closure}() must implement interface Psr\Http\Message\ResponseInterface, instance of Slim\Psr7\Request given, called in /home/martyn/APP/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php on line 42 and defined in /home/martyn/APP/src/routes/users.php:72

Stack trace:

#0 /home/martyn/APP/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(42): {closure}(Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array)

#1 /home/martyn/APP/vendor/slim/slim/Slim/Routing/Route.php(372): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array)

#2 /home/martyn/APP/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): Slim\Routing\Route->handle(Object(Slim\Psr7\Request)) #3 /home/martyn/APP/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))

#4 /home/martyn/APP/vendor/slim/slim/Slim/Routing/Route.php(333): Slim\MiddlewareDispatche in /home/martyn/APP/src/routes/users.php on line 72

se eliminaron 2500 caracteres en el cuerpo
Origen Enlace
Martín LC
  • 369
  • 1
  • 9
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use DI\Container;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$app->get('/app/users', function(Request $request, Response $response){
    $sql = "SELECT * FROM users";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->query($sql);
        $users = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        $response->getBody()->write(json_encode($users));
        return $response;
    } catch(PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->get('/app/user/{id}', function(Request $request, Response $response){
    $id = $request->getAttribute('id');
    $sql = "SELECT * FROM users WHERE id = '$id'";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->query($sql);
        $user = $stmt->fetchAll(PDO::FETCH_OBJ);
        $response->getBody()->write(json_encode($user));
        return $response;
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->post('/app/user/add', function(Request $request, Response $response){
    $user= json_decode($request->getBody()->getContents(), true);
    $sql = "INSERT INTO users set user_name=:user_name, lastName=:lastName, email=:email";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->bindValue(':user_name', $user['user_name']);
        $stmt->bindValue(':lastName',  $user['lastName']);
        $stmt->bindValue(':email',     $user['email']);
        $stmt->execute($user);
        echo '{"notice": "Usuario agregado"}';
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->put('/app/user/update/{id}', function(Request $request, Response $response){
    $id = $request->getAttribute('id');
    $user= json_decode($request->getBody()->getContents(), true);
    $sql = "UPDATE users set user_name = :user_name, lastName = :lastName, email = :email WHERE id = $id";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute($user);
        echo '{"notice": "Usuario modificado"}';
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->delete('/app/user/delete/{id}', function (Response $response, Request $request, $args){
    $id = $args['id'];
    $sql = "DELETE FROM users WHERE id = '$id'";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute([$id]);
        $db = null;
        echo '{"notice": "Usuario borrado con éxito"}';
    }catch (PDOException $e) {
        echo '{"error": {"text": ' . $e->getMessage() . '}';
    }
});
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use DI\Container;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$app->get('/app/users', function(Request $request, Response $response){
    $sql = "SELECT * FROM users";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->query($sql);
        $users = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        $response->getBody()->write(json_encode($users));
        return $response;
    } catch(PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->get('/app/user/{id}', function(Request $request, Response $response){
    $id = $request->getAttribute('id');
    $sql = "SELECT * FROM users WHERE id = '$id'";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->query($sql);
        $user = $stmt->fetchAll(PDO::FETCH_OBJ);
        $response->getBody()->write(json_encode($user));
        return $response;
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->post('/app/user/add', function(Request $request, Response $response){
    $user= json_decode($request->getBody()->getContents(), true);
    $sql = "INSERT INTO users set user_name=:user_name, lastName=:lastName, email=:email";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->bindValue(':user_name', $user['user_name']);
        $stmt->bindValue(':lastName',  $user['lastName']);
        $stmt->bindValue(':email',     $user['email']);
        $stmt->execute($user);
        echo '{"notice": "Usuario agregado"}';
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->put('/app/user/update/{id}', function(Request $request, Response $response){
    $id = $request->getAttribute('id');
    $user= json_decode($request->getBody()->getContents(), true);
    $sql = "UPDATE users set user_name = :user_name, lastName = :lastName, email = :email WHERE id = $id";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute($user);
        echo '{"notice": "Usuario modificado"}';
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->delete('/app/user/delete/{id}', function (Response $response, Request $request, $args){
    $id = $args['id'];
    $sql = "DELETE FROM users WHERE id = '$id'";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute([$id]);
        $db = null;
        echo '{"notice": "Usuario borrado con éxito"}';
    }catch (PDOException $e) {
        echo '{"error": {"text": ' . $e->getMessage() . '}';
    }
});
$app->delete('/app/user/delete/{id}', function (Response $response, Request $request, $args){
    $id = $args['id'];
    $sql = "DELETE FROM users WHERE id = '$id'";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute([$id]);
        $db = null;
        echo '{"notice": "Usuario borrado con éxito"}';
    }catch (PDOException $e) {
        echo '{"error": {"text": ' . $e->getMessage() . '}';
    }
});
se añadieron 2424 caracteres en el cuerpo
Origen Enlace
Martín LC
  • 369
  • 1
  • 9
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use DI\Container;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$app->delete>get('/app/userusers', function(Request $request, Response $response){
    $sql = "SELECT * FROM users";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->query($sql);
        $users = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        $response->getBody()->write(json_encode($users));
        return $response;
    } catch(PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->get('/deleteapp/user/{id}', function (ResponseRequest $response$request, RequestResponse $request$response){
    $id = $request->getAttribute('id');
    $sql = "DELETE"SELECT * FROM users WHERE id = '$id'";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->query($sql);
        $user = $stmt->fetchAll(PDO::FETCH_OBJ);
        $response->getBody()->write(json_encode($user));
        return $response;
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->post('/app/user/add', function(Request $request, Response $response){
    $user= json_decode($request->getBody()->getContents(), true);
    $sql = "INSERT INTO users set user_name=:user_name, lastName=:lastName, email=:email";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->bindValue(':user_name', $user['user_name']);
        $stmt->>bindValue(':lastName', execute $user['lastName']);
        $stmt->bindValue(':email',     $user['email']);
        $stmt->execute($user);
        echo '{"notice": "Usuario agregado"}';
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->put('/app/user/update/{id}', function(Request $request, Response $response){
    $id = $request->getAttribute('id');
    $user= json_decode($request->getBody()->getContents(), true);
    $sql = "UPDATE users set user_name = :user_name, lastName = :lastName, email = :email WHERE id = $id";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute($user);
        echo '{"notice": "Usuario modificado"}';
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->delete('/app/user/delete/{id}', function (Response $response, Request $request, $args){
    $id = $args['id'];
    $sql = "DELETE FROM users WHERE id = '$id'";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute([$id]);
        $db = null;
        echo '{"notice": "Usuario borrado con éxito"}';
    }catch (PDOException $e) {
        echo '{"error": {"text": ' . $e->getMessage() . '}';
    }
});
 
$app->delete('/app/user/delete/{id}', function (Response $response, Request $request){
    $id = $request->getAttribute('id');
    $sql = "DELETE FROM users WHERE id = '$id'";
    $user= json_decode($request->getBody()->getContents(), true);
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt -> execute($user);
        $db = null;
        echo '{"notice": "Usuario borrado con éxito"}';
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});
 
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use DI\Container;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$app->get('/app/users', function(Request $request, Response $response){
    $sql = "SELECT * FROM users";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->query($sql);
        $users = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        $response->getBody()->write(json_encode($users));
        return $response;
    } catch(PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->get('/app/user/{id}', function(Request $request, Response $response){
    $id = $request->getAttribute('id');
    $sql = "SELECT * FROM users WHERE id = '$id'";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->query($sql);
        $user = $stmt->fetchAll(PDO::FETCH_OBJ);
        $response->getBody()->write(json_encode($user));
        return $response;
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->post('/app/user/add', function(Request $request, Response $response){
    $user= json_decode($request->getBody()->getContents(), true);
    $sql = "INSERT INTO users set user_name=:user_name, lastName=:lastName, email=:email";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->bindValue(':user_name', $user['user_name']);
        $stmt->bindValue(':lastName',  $user['lastName']);
        $stmt->bindValue(':email',     $user['email']);
        $stmt->execute($user);
        echo '{"notice": "Usuario agregado"}';
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->put('/app/user/update/{id}', function(Request $request, Response $response){
    $id = $request->getAttribute('id');
    $user= json_decode($request->getBody()->getContents(), true);
    $sql = "UPDATE users set user_name = :user_name, lastName = :lastName, email = :email WHERE id = $id";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute($user);
        echo '{"notice": "Usuario modificado"}';
    }catch (PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

$app->delete('/app/user/delete/{id}', function (Response $response, Request $request, $args){
    $id = $args['id'];
    $sql = "DELETE FROM users WHERE id = '$id'";
    try{
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute([$id]);
        $db = null;
        echo '{"notice": "Usuario borrado con éxito"}';
    }catch (PDOException $e) {
        echo '{"error": {"text": ' . $e->getMessage() . '}';
    }
});
se añadieron 29 caracteres en el cuerpo
Origen Enlace
Xerif
  • 7.4k
  • 3
  • 19
  • 42
Loading
Se hace visible los errores y las tecnologias en la que esta trabajando
Origen Enlace
Loading
se añadieron 1131 caracteres en el cuerpo
Origen Enlace
Martín LC
  • 369
  • 1
  • 9
Loading
Origen Enlace
Martín LC
  • 369
  • 1
  • 9
Loading