无法在Slim PHP中发布参数

无法在Slim PHP中发布参数,php,mysql,slim,Php,Mysql,Slim,今天我开始学习slim php,这样我就可以开始post并将数据保存到mysql。我现在遇到的问题是,我试图将下面的示例代码用于此urlhttp://localhost/MyRest/index.php/updateScore?id=1&score=36 但无法发送参数。如果可能的话,你能给我一个样品或小费吗?我很想听到你的消息 $app->post('/updateScore', function() use($app){ $allPostVars = $app->requ

今天我开始学习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

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

请更改为:

 $allPostVars = $app->request->post();
为此:

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

请参考以下内容:

谢谢!我有一个问题,可以在url中使用$app->post('/updateScore')吗?或者它只是用来通过代码发送参数的?对不起,我没听清楚。你到底想要什么?我只是想使用一个url,比如,这样我就可以将参数传递给db,但是,我无法用上面的代码发送它,所以get()是唯一将参数传递给url的解决方案吗?你也可以这样做:
$app->get('/updateScore',function($id,$score)