在查找正确的循环以检查mysql中是否包含了一些值时,我遇到了一些麻烦。
我正在制作一个软件,我想要添加许可证ID。每个用户都有x个密钥可供使用。
现在,当用户启动客户机时,它会调用一个php页面,检查POST方法中发送的密钥是否存储在DB中。
如果那把钥匙不是存储的,我需要检查他的钥匙的号码。如果大于X,我将禁止他,否则我会在DB中添加新的密钥。
我是PHP和MYSQL的新手。我写了这段代码,如果我能改进它,我会知道的。
<!-- language: php -->
....
$user = POST METHOD
$licenseID = POST METHOD
....
....
$resultLic= mysql_query("SELECT id , idUser , idLicense FROM license WHERE idUser = '$user'") or die(mysql_error());
$resultNumber = mysql_num_rows($resultLic);
$keyFound = '0'; // If keyfound is 1 the key is stored in DB
while ($rows = mysql_fetch_array($resultLic,MYSQL_BOTH)) {
//this loop check if the $licenseID is stored in DB or not
for($i=0; $i< $resultNumber ; i++)
{
if($rows['idLicense'] === $licenseID) {
//Just for the debug
echo("License Found");
$keyFound = '1';
break;
}
//If key isn't in DB and there are less than 3 keys the new key will be store in DB
if($keyfound == '0' && $resultNumber < 3){
mysql_query(Update users set ...Store $licenseID in Table) }
// Else mean that the user want user another generated key (from the client) in the DB and i will be ban
(It's wrote in TOS terms that they cant use the software on more than 3 different station)
else { mysql_query(update users set ban ='1'.....etc)
}
}我知道这个代码看起来很糟糕,所以我知道如何改进它。有人能给我什么建议吗?
更新:--我忘记说我选择了两个表--一个是用户的内容--所有关于用户id、用户名、密码等的信息,另一个是我获得id、idUsername、idLicense (软件生成的最后一个存储许可)的表许可证。
谢谢
发布于 2011-11-15 17:13:13
我可能错了,但是为什么不用这个SQL查询向Mysql发出LicencsId搜索呢?
"SELECT id , idUser , idLicense FROM license WHERE idUser = '$user' AND idLicense='$licenseID'
出于安全原因,可以转义输入"SELECT id , idUser , idLicense FROM license WHERE idUser = " . mysql_real_escape_string($user) . " AND idLicense=" . mysql_real_escape_string($licenseID)
https://stackoverflow.com/questions/8140129
复制相似问题