I am trying to update an object of my bd
through my API
, I am using Slim Framework 3
but when I execute the postman
I get the following error:
Can not use object of type stdClass as array
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app->group('/persona/', function () {
$app = new \Slim\App;
$this->put('editar/{id}', function (Request $request, Response $response) {
$input = json_decode($request->getBody());
$array = (array)$input;
$nombre = $array['nombre'];
$apellido = $array['apellido'];
$id = $array['id'];
$sql = "UPDATE persona SET nombre=:nombre, apellido=:apellido, id=:id WHERE id=$id";
try {
$db = new db();
$db = $db->connect();
$sth = $db->prepare($sql);
$sth->bindParam('nombre', $nombre);
$sth->bindParam('apellido', $apellido);
$sth->bindParam('id', $id);
$sth->execute();
$db = null;
echo '{"notice": {"text": "Persona Actualizada"}';
} catch(PDOException $e) {
echo '{"error": {"text": '.$e->getMessage(). '}';
}
});
});