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

错误: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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题