无法在 Slim PHP 中使用 post 参数
Unable to post parameters in Slim PHP
今天我开始学习 slim php 以便我可以开始 post 并将数据保存到 mysql。我现在遇到的问题是我尝试将下面的示例代码与此 url http://localhost/MyRest/index.php/updateScore?id=1&score=36
一起使用
但无法发送参数。如果可能的话,你能给我一份样品或小费吗?我很想听听你的意见!
$app->post('/updateScore', function() use($app){
$allPostVars = $app->request->post();
$score = $allPostVars['score'];
$id = $allPostVars['id'];
try
{
$db = getDB();
$sth = $db->prepare("UPDATE students
SET score = :score
WHERE student_id = :id");
$sth->bindParam(':score', $score, PDO::PARAM_INT);
$sth->bindParam(':id', $id, PDO::PARAM_INT);
$sth->execute();
$app->response->setStatus(200);
$app->response()->headers->set('Content-Type', 'application/json');
echo json_encode(array("status" => "success", "code" => 1));
$db = null;
} catch(PDOException $e) {
$app->response()->setStatus(404);
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
});
来自 url : http://localhost/MyRest/index.php/updateScore?id=1&score=36
您正在发送参数作为查询 string.So 它不会进入 post
方法。你需要 get
方法。
请更改为:
$allPostVars = $app->request->post();
为此:
$allPostVars = $app->request->get();
今天我开始学习 slim php 以便我可以开始 post 并将数据保存到 mysql。我现在遇到的问题是我尝试将下面的示例代码与此 url http://localhost/MyRest/index.php/updateScore?id=1&score=36
一起使用
但无法发送参数。如果可能的话,你能给我一份样品或小费吗?我很想听听你的意见!
$app->post('/updateScore', function() use($app){
$allPostVars = $app->request->post();
$score = $allPostVars['score'];
$id = $allPostVars['id'];
try
{
$db = getDB();
$sth = $db->prepare("UPDATE students
SET score = :score
WHERE student_id = :id");
$sth->bindParam(':score', $score, PDO::PARAM_INT);
$sth->bindParam(':id', $id, PDO::PARAM_INT);
$sth->execute();
$app->response->setStatus(200);
$app->response()->headers->set('Content-Type', 'application/json');
echo json_encode(array("status" => "success", "code" => 1));
$db = null;
} catch(PDOException $e) {
$app->response()->setStatus(404);
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
});
来自 url : http://localhost/MyRest/index.php/updateScore?id=1&score=36
您正在发送参数作为查询 string.So 它不会进入 post
方法。你需要 get
方法。
请更改为:
$allPostVars = $app->request->post();
为此:
$allPostVars = $app->request->get();