i'm working with slim framework this my post rute code:
$app->post("/notas/", function() use($app){
$titulo = $app->request->post("titulo");
$intro = $app->request->post("intro");
$contenido = $app->request->post("contenido");
$author = $app->request->post("autor");
try{
$connection = getConnection();
$dbh = $connection->prepare("INSERT INTO post VALUES(null, ?, ?, ?, ?) ");
$dbh->bindValue(1, $titulo);
$dbh->bindValue(2, $intro);
$dbh->bindValue(3, $contenido);
$dbh->bindValue(4, $author);
$dbh->execute();
$notasId = $connection->lastInsertId();
$connection = null;
$app->response->headers->set("Content-type", "application/json");
$app->response->status(200);
$app->response->body(json_encode(
array(
"post id @ " => $notasId
)
)
);
}
catch(PDOException $e){
echo "Error: " . $e->getMessage();
}
});
Using curl
curl -X POST -d "titulo=text text&intro=text text text text&autor=text" http://192.168.30.10/v1/notas
I have this:
Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'contenido' cannot be null
I dont know what's wrong :(
Edit:
Post table:
CREATE TABLE IF NOT EXISTS `post` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`titulo` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`intro` varchar(300) COLLATE utf8_unicode_ci NOT NULL,
`contenido` longtext COLLATE utf8_unicode_ci NOT NULL,
`autor` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;
There are 2 reasons for this.
The first and most important here seems to be that you're not sending
contenido
value in your curl request:and you also defined your
post.contenido
asNOT NULL
, so MySQL gives you error, not founding a value for that column.The second is the one that Mureinik explained (supply the columns names in the
INSERT
statement):