# Can´t insert data in POST method

**URL:** <https://discourse.slimframework.com/t/can-t-insert-data-in-post-method/4270>\
**Category:** Questions\
**Created:** [July 7, 2020, 8:46pm UTC](https://discourse.slimframework.com/t/can-t-insert-data-in-post-method/4270 "2020-07-07T20:46:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mAlarcon](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@mAlarcon](https://discourse.slimframework.com/u/mAlarcon)\
**Post date:** [July 7, 2020, 8:46pm UTC](https://discourse.slimframework.com/t/can-t-insert-data-in-post-method/4270/1 "2020-07-07T20:46:26Z")

</div>

Hi, i am very new on this Frame, i am creating REST Api and now i am trying to insert data in a MYSQL DB with the POST method, but i can´t this is mi structure method:

$app-\>post(’/api/clientes/nuevo’, function (Request $request, Response $response) {

```
$nombre = $request->getServerParams('nombre'); //String
$apellidos = $request->geServerParams('apellidos');
$telefono = $request->getServerParams('telefono');
$email = $request->getServerParams('email');
$direccion = $request->getServerParams('direccion');
$ciudad = $request->getServerParams('ciudad');

$sql = "INSERT INTO clientes(nombre, apellidos, telefono, email, direccion, ciudad) VALUES (:nombre, :apellidos, :telefono, :email, :direccion, :ciudad)";

try {
    $db = new db();
    $db = $db->conectDB();
    $resultado = $db->prepare($sql);

    $resultado->bindParam(':nombre', $nombre);
    $resultado->bindParam(':apellidos', $apellidos);
    $resultado->bindParam(':telefono', $telefono);
    $resultado->bindParam(':email', $email);
    $resultado->bindParam(':direccion', $direccion);
    $resultado->bindParam(':ciudad', $ciudad);

    $resultado->execute();

   $response->getBody()->write("Nuevo cliente añadido");
   return $response;

    $resultado = null;
    $db = null;

} catch (PDOException $e) {

    echo '{"error" : {"text":' . $e->getMessage() . '}';
}

```

});

And this is my DB connection:

class db{

```
private $dbHost = 'localhost';
private $dbUser = 'root';
private $dbPass = '';
private $dbName = 'apirest';

public function conectDB(){
    $mysqlconnect = "mysql:host=$this->dbHost;dbname=$this->dbName";
    $dbConnecion = new PDO($mysqlconnect,$this->dbUser, $this->dbPass);
    $dbConnecion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $dbConnecion;
}

```

}

I think one of the manin problems is how i want access to the data or how a define the Request

---

<div class="post-metadata">

**Author:** ![dafriend](https://avatars.discourse-cdn.com/v4/letter/d/da6949/32.png) [@dafriend](https://discourse.slimframework.com/u/dafriend)\
**Post date:** [July 7, 2020, 11:28pm UTC](https://discourse.slimframework.com/t/can-t-insert-data-in-post-method/4270/2 "2020-07-07T23:28:29Z")

</div>

I’m new to Slim so cut me some slack if this is wrong.

I think you’re looking for something like this.

```
// Collect input from the HTTP request
$data = (array)$request->getParsedBody();

$nombre = $data['nombre'];
$apellidos = $data['apellidos';
// and all the rest of the inputs

```

The rest is fine as far as I can tell.

---

<div class="post-metadata">

**Author:** ![mAlarcon](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@mAlarcon](https://discourse.slimframework.com/u/mAlarcon)\
**Post date:** [July 8, 2020, 2:15pm UTC](https://discourse.slimframework.com/t/can-t-insert-data-in-post-method/4270/3 "2020-07-08T14:15:08Z")

</div>

Thanks so, I made that changes, and now when I send the request In my api cliente I recive this response:

**Uncaught Slim\Exception\HttpMethodNotAllowedException: Method not allowed. Must be one of: GET**

I think this problem gives me cause I am using post and get to recive the data, I explore some topics here and found that maybe I have to **Setting CORS**. But I`m not sure
