PHP和基本的RESTful服务; GET请求所有记录与一个记录 [英] PHP and basic RESTful services; GET requesting all records vs one record

查看:133
本文介绍了PHP和基本的RESTful服务; GET请求所有记录与一个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PHP类有一个涉及RESTful服务的项目,我们有这样一个基本服务:

  $ request = $ _SERVER [REQUEST_METHOD]; 
switch($ request){
case'GET':
$ user = $ acctReader-> read($ _ GET [username],$ _GET [password]);
if(isset($ _ GET [id])&& $ user == true){
// echo方法在验证$ user是帐户后返回单个记录一个单独的表
} elseif(!isset($ _ GET [id]&& $ user == true){
// echo方法从db
返回所有记录}
case'POST':
case'PUT':
case'DELETE':
default
}

我正在让GET响应用户验证,因此如果他们输入了有效的用户名/密码但显式请求一个单一的ID,他们应该看到所有的记录

这是我的HTML的一部分:

 < form action =TaskService.phpmethod =GET> 
< label for =username>用户名< / label>
<输入类型=textname =usernameid =usernamerequired />< br />>
< label for =pass字>密码< /标签>
< input type =passwordname =passwordid =passwordrequired />< br />
< label for =id>任务ID< / label>
< input type =textname =idid =idplaceholder =optional/>
< input type =submitname =getTaskvalue =GET TASK/>
< / form>

我能够验证我的第一个条件(输入用户名,密码和id) )它返回一个值。然而,当我没有传入id时,我无法让所有的记录都存在。
关于我应该在哪里进行更改的任何想法?

解决方案

即使它是空的, $ _ GET [id] 也会被设置。如果(!)($ _GET [id])&& $ user = $,您将不得不要求我们

  = true){
// echo方法在验证$ user是单独表中的帐户后返回单个记录
} else {
// echo方法返回所有记录db
}



  if(isset($ _GET [id])&& $ _GET [id]!=''&& $ user == true){
// echo方法在验证$ user是一个单独表中的帐户之后从db返回单个记录
} else {
// echo方法从db
返回所有记录}




但是要小心使用 empty()当字段可能包含

$ ul
<0> 0(0作为一个整数)

  • 0.0(0作为浮点数)
  • 0(0作为字符串)


    因为这些都被认为是空的

    查看手册



  • My PHP class has a project involving RESTful services where we have a basic service set up like this:

    $request = $_SERVER["REQUEST_METHOD"];
    switch($request) {
        case 'GET':
            $user = $acctReader->read($_GET["username"], $_GET["password"]);
            if (isset($_GET["id"]) && $user == true) {
                // echo method to return single record from db after validating that $user is an account in a separate table
            } elseif (!isset($_GET["id"] && $user == true) {
                // echo method to return ALL records from db
            }
        case 'POST':
        case 'PUT':
        case 'DELETE':
        default
    }
    

    I'm in the midst of having the GET to respond to user validation in such that if they enter in a valid username/password but do not explicitly request a single id, they should see all the records
    Here's what part of my html looks like:

    <form action="TaskService.php" method="GET">
            <label for="username">Username</label>
            <input type="text" name="username" id="username" required/><br />
            <label for="password">Password</label>
            <input type="password" name="password" id="password" required/><br />
            <label for="id">Task ID</label>
            <input type="text" name="id" id="id" placeholder="optional"/>
            <input type="submit" name="getTask" value="GET TASK"/>
    </form>
    

    I was able to verify that my first conditional (where the username, pw, and an id are entered) that it returns one value. However, I am having trouble with having it give me all records when I don't pass an id in.
    Any ideas on where I should be making my changes?

    解决方案

    The $_GET["id"] will be SET even if it is empty. You will have to either us

    if (!empty($_GET["id"]) && $user == true) {
        // echo method to return single record from db after validating that $user is an account in a separate table
    } else {
        // echo method to return ALL records from db
    }
    

    Or

    if (isset($_GET["id"]) && $_GET["id"] != '' && $user == true) {
        // echo method to return single record from db after validating that $user is an account in a separate table
    } else {
        // echo method to return ALL records from db
    }
    

    But beware of using empty() when the field could contains

    • 0 (0 as an integer)
    • 0.0 (0 as a float)
    • "0" (0 as a string)

    as these are all considered as empty

    See the manual

    这篇关于PHP和基本的RESTful服务; GET请求所有记录与一个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆