It turns out that I have the following method where I receive a JSON and get the values of its items by sending to save to the fields of a database
Method that receives JSON
<?php
require 'meta.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$body = json_decode(file_get_contents("php://input"), true);
$retorno = meta::insert(
$body['titulo'],
$body['descripcion'],
$body['fechaLim'],
$body['categoria'],
$body['prioridad']);
if ($retorno) {
print json_encode(array(
'estado' => '1',
'mensaje' => 'Creacion exitosa'
));
}
else
{
print json_encode(array(
'estado' => '2',
'mensaje' => 'Creación fallida'
));
}
}?>
in the meta.php file, I have the following insert method to send to the database.
public static function insert($titulo,$descripcion,$fechaLim,$categoria,$prioridad)
{
$comando = "INSERT INTO meta(".
"titulo,".
"descripcion,".
"fechaLim,".
"categoria,".
"prioridad)".
"VALUES( ?,?,?,?,?)";
$sentencia = database::getInstance()->getDb()->prepare($comando);
return $sentencia->execute(
array(
$titulo,
$descripcion,
$fechaLim,
$categoria,
$prioridad
)
);
}
I have the question of How to send the parameters by web service to save in the database? I'm using postman to test but I do not know what way to do it.
When performing the test in postman gives the following error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE [23000]: Integrity constraint violation: 1048 Column' title 'can not be null' in C: \ xampp \ htdocs \ i_wish \ php \ meta.php: 67 Stack trace: # 0 C: \ xampp \ htdocs \ i_wish \ php \ meta.php (67): PDOStatement-> execute (Array) # 1 C: \ xampp \ htdocs \ i_wish \ php \ obtain_metas.php (59): meta :: insert (NULL, NULL, NULL, NULL, NULL) # 2 {main} thrown in C: \ xampp \ htdocs \ i_wish \ php \ meta.php on line 67