如何使用slimframework 3在数组中存储已发布的参数

时间:2017-08-23 11:27:25

标签: php arrays slim slim-3

我想将发布的参数存储到数组中的函数中, 我正在使用slim 3

我的问题

  1. request->getparam('')我应该如何获取参数 张贴?

  2. 我应该绑定它们吗?如果是这样,我应该约束他们,因为我 不在任何地方使用:Adminusera :Adminuserb :Adminuserc

  3. 我可以在我的数组值:Adminusera而不是$userA中放置一个方法吗?

  4. 我的代码:

    //IS THIS HOW I GET THE POSTED PARAMETERS To My Function ?
    $userA = $request->getParam('usera');   
    $userB = $request->getParam('userb');
    $userC = $request->getParam('userc');
    $sql = "SELECT *FROM admins";
    try {
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':Adminusera', $userA);
        $stmt->bindParam(':Adminuserb', $userB);
        $stmt->bindParam(':Adminuserc', $userC);
        $stmt->execute();
        $admin = $stmt->fetch(PDO::FETCH_OBJ);
        $db = null;
        if(!empty($admin)){
            $newUsers = array('a' => $userA, 'b' => $userB, 'c' => $userC);
            print_r($newUsers); 
        }
    }
    

1 个答案:

答案 0 :(得分:0)

原来我不需要使用bindparam我可以像这样做

$userA = $request->getParam('usera');   
$userB = $request->getParam('userb');
$userC = $request->getParam('userc');
$sql = "SELECT *FROM admins";
try {
    $db = new db();
    $db = $db->connect();
    $stmt = $db->prepare($sql);

    $stmt->execute();
    $admin = $stmt->fetch(PDO::FETCH_OBJ);
    $db = null;
    if(!empty($admin)){
        $newUsers = array('a' => $userA, 'b' => $userB, 'c' => $userC);
        print_r($newUsers); 
    }
}

因此我需要使用x-www-form-urlencoded

将post请求发送到我的函数