gpt4 book ai didi

php - 无法在 Slim PHP 中发布参数

转载 作者:行者123 更新时间:2023-11-29 06:16:46 24 4
gpt4 key购买 nike

今天我开始学习 slim php 以便我可以开始发布和保存数据到 mysql。我现在遇到的问题是我尝试将下面的示例代码与此 url http://localhost/MyRest/index.php/updateScore?id=1&score=36 一起使用但无法发送参数。如果可能的话,你能给我一份 sample 或小费吗?我很想听听你的意见!

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

});

最佳答案

来自网址:http://localhost/MyRest/index.php/updateScore?id=1&score=36

您正在将参数作为查询字符串发送。因此它不会进入 post 方法。你需要 get 方法。

请更改为:

 $allPostVars = $app->request->post();

为此:

 $allPostVars = $app->request->get();

请引用:http://docs.slimframework.com/request/variables/

关于php - 无法在 Slim PHP 中发布参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35437238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com