Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'contenido' cannot be null

2.6k views Asked by At

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 ;
2

There are 2 answers

0
Davide Pastore On

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:

curl -X POST -d "titulo=text text&intro=text text text text&autor=text&contenido=your contenido text" http://192.168.30.10/v1/notas 

and you also defined your post.contenido as NOT 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):

$dbh = $connection->prepare
       ("INSERT INTO post (titulo, intro, contenido, author) VALUES (?, ?, ?, ?)");

$dbh->bindValue(1, $titulo);
$dbh->bindValue(2, $intro);
$dbh->bindValue(3, $contenido);
$dbh->bindValue(4, $author);
1
Mureinik On

If you do not supply the column names, MySQL just takes them according to the order they're defined in the table, so $titulo is used for id, $intro for titulo, etc. In order to allow the database to autogenerate id, you need to provide a list of column names, and explicitly exclude it:

$dbh = $connection->prepare
       ("INSERT INTO post (titulo, intro, contenido, author) VALUES (?, ?, ?, ?)");

$dbh->bindValue(1, $titulo);
$dbh->bindValue(2, $intro);
$dbh->bindValue(3, $contenido);
$dbh->bindValue(4, $author);