rest - PHP and basic RESTful services; GET requesting all records vs one record -


my php class has project involving restful services have basic service set this:

$request = $_server["request_method"]; switch($request) {     case 'get':         $user = $acctreader->read($_get["username"], $_get["password"]);         if (isset($_get["id"]) && $user == true) {             // echo method return single record db after validating $user account in separate table         } elseif (!isset($_get["id"] && $user == true) {             // echo method return records db         }     case 'post':     case 'put':     case 'delete':     default } 

i'm in midst of having respond user validation in such if enter in valid username/password do not explicitly request single id, should see records
here's part of 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 able verify first conditional (where username, pw, , id entered) returns 1 value. however, having trouble having give me all records when don't pass id in.
ideas on should making changes?

the $_get["id"] set if empty. have either us

if (!empty($_get["id"]) && $user == true) {     // echo method return single record db after validating $user account in separate table } else {     // echo method return records db } 

or

if (isset($_get["id"]) && $_get["id"] != '' && $user == true) {     // echo method return single record db after validating $user account in separate table } else {     // echo method return records db } 

but beware of using empty() when field contains

  • 0 (0 integer)
  • 0.0 (0 float)
  • "0" (0 string)

as these considered empty

see manual


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -