【问题标题】:Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'contenido' cannot be null错误:SQLSTATE [23000]:违反完整性约束:1048 列“contenido”不能为空
【发布时间】:2015-06-07 21:27:51
【问题描述】:

我正在使用超薄框架,这是我的帖子代码:

$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();
     }
});

使用卷曲

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

我有这个:

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

我不知道怎么了:(

编辑:

张贴表:

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 ;

【问题讨论】:

  • 你能分享post表的定义吗?
  • 尝试传递您要插入的列名。你也可能不需要那个null
  • 使用insert 语句时,始终列出insert 的所有列。我猜这会解决你的问题。除非第一列是 contenido 并且 它被声明为 not null 并且它不是自动递增列。您还应该使用您正在使用的数据库标记问题。
  • @GordonLinoff 天哪,你今天突破了 300k 大关 :) 继续你的好答案。
  • @Mureinik 这是我的帖子表...

标签: php mysql pdo slim


【解决方案1】:

如果你不提供列名,MySQL 只是按照它们在表中定义的顺序来获取它们,所以$titulo 用于id$intro 用于titulo,等等。为了让数据库自动生成id,您需要提供一个列名列表,并明确排除它:

$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);

【讨论】:

  • SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'contenido' cannot be null
【解决方案2】:

这有两个原因。

这里的第一个也是最重要的似乎是您没有在 curl 请求中发送 contenido 值:

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 

并且您还将post.contenido 定义为NOT NULL,因此MySQL 给您错误,没有为该列找到值。

第二个是Mureinik explained(在INSERT 语句中提供列名):

$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);

【讨论】:

    猜你喜欢
    • 2021-02-01
    • 1970-01-01
    • 2019-11-20
    • 2021-08-16
    • 2019-10-06
    • 2020-10-25
    • 2017-02-28
    • 2017-04-15
    • 2017-12-26
    相关资源
    最近更新 更多