doxrxwn2252 2015-06-07 21:27
浏览 249

错误:SQLSTATE [23000]:完整性约束违规:1048列'contenido'不能为空

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条回答 默认 最新

  • dongmu5246 2015-06-07 21:59
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题