我正在使用slim框架这个我的帖子rute代码:
$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
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 ;
答案 0 :(得分:0)
如果您不提供列名,MySQL只会根据他们在表中定义的顺序来接收它们,因此$titulo
用于id
,$intro
for 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);
答案 1 :(得分:0)
这有两个原因。
这里的第一个也是最重要的似乎是你没有在你的卷曲请求中发送Starting tunnel...
Initialised iOS on MAC 8.1
Test main - Suite Name - Login Screen FAILED on iOS on MAC 8.1:
UnknownCommand: [POST http://192.168.158:4723/wd/hub/session/410a4e1e-b886-4e2a-aec0-f1c5178577e4/ime/deactivate] **Not yet implemented**
值:
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
,因此MySQL会为您提供错误,而不是为该列创建值。
第二个是Mureinik explained(在NOT NULL
语句中提供列名称):
INSERT