如何建立一个id检查器


How to build a id checker

在尝试了我自己之后,我有点失败,我基本上需要建立一个PHP + SQL系统,所有我通过的是一个ID进入它,看看这个ID的许可证是否已经过期。

如果未过期,则将其许可证向下重写1。(许可证应该从20开始)

基本上是:(从android设备通过一个字符串ID连接后)

    连接数据库
  1. 连接到表(表有ID和License字段)
  2. 检查ID是否在数据库
  3. 如果id在数据库中,则
  4. 检查其许可证
    • 如果许可证为0,则停止所有操作并返回false
    • 如果license> 0,则license-1并返回"true"
  5. 如果ID不在数据库
  6. 则将ID插入到许可值为20的表中,返回true

我已经很久没有使用标准的mysql函数调用了,所以请原谅我。

//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;
}
伪代码
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
}