asd2005
asd2005

Reputation: 315

How to build a id checker

After trying my self I'm kinda failing, I basically need to build an PHP + SQL system where all i pass is an ID into it and see if that id's license has expired.

if it hasn't expired then rewrite its license down by 1. (license should starts at 20)

So basically: (After connected from an android device passing a String ID)

  1. connect to the database
  2. connect to the table ( table has ID and License fields)
  3. check if the ID is within the database
  4. If the id is within the database then
  5. check its license
    • if its license is = 0, then stop everything and return back with "false"
    • if the license is > 0, then license-1 and return back "true"
  6. if the ID is not within the database
  7. then insert the ID into the table with license value at 20 and return true

Upvotes: 1

Views: 243

Answers (2)

Galen
Galen

Reputation: 30170

pseudo code

query = select license from table where id=:id
if count(query) {
    if (license <= 0) {
        return false
    }
    license--;
    update table set license=:license where id=:id
    return true
}
else {
    insert into table(id, license) values(:id, 20)
    return true
}

Upvotes: 0

Jaime
Jaime

Reputation: 1410

I haven't used the standard mysql function calls in a long time, so bear with me.

//Connect stuff has been done...
$query = "SELECT ID, license
          FROM table
          WHERE ID = '$someID'";
$result = query($query);
if($result->rowCount() > 0){
  $row = $result->fetch();
  if($row['license'] == 0)
    return false;
  else{
    $license = $row['license'] - 1;
    $query = "UPDATE table SET license = '$license' WHERE ID = '$someID'";
    query($query);
    return true;
  }
}
else{
   $query = "INSERT INTO table(ID, license) VALUES('$someID', 20)";
   query($query);
   return true;
}

Upvotes: 1

Related Questions