【发布时间】: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 这是我的帖子表...