php - 我的逻辑有什么问题?尝试制作一个简单的mysqli脚本
问题描述
我正在尝试为我的 FluxBB 论坛制作一个 PHP 脚本,该脚本使用用户提供的激活密钥来升级它们并为他们提供有效订阅。当我单击“激活”按钮时,没有任何反应。我想我的逻辑有问题,可能是什么?
我尝试了多种方法,例如更改逻辑和简化代码、调试。但我被这个问题困住了。
if(isset($_POST['activate']))
{
// $pun_user['id'] is User ID
// csgo is subscription ending time
$motify_checksub = "SELECT `csgo` FROM `".$db->prefix."users` WHERE `id` = ".$pun_user['id'] or error('[Motify] Unable to check subscription', __FILE__, __LINE__, $db->error());
$db->query($motify_checksub);
$motify_sub_result = $db->fetch_assoc($motify_checksub);
$motify_now = date("Y-m-d H:i:s");
// has active subscription already?
if ($motify_sub_result > $motify_now)
{
$motify_akey = $db->escape($_POST['key']);
// check if activation key is valid and not used
$motify_check_key = $db->query("SELECT COUNT(*) FROM `".$db->prefix."keys` WHERE `akey` = '".$motify_akey."' AND `used` = '0'") or error('[Motify] Unable to validate activation key', __FILE__, __LINE__, $db->error());
$motify_key_result = $db->num_rows($motify_keycheck);
if ($motify_key_result > 0)
{
// check key value (30 days, 90 days or 365 days)
$motify_checklength = "SELECT `sub` FROM `".$db->prefix."keys` WHERE `akey` = '".$motify_akey or error('[Motify] Unable to subscription length', __FILE__, __LINE__, $db->error());
$db->query($motify_checklength);
$motify_length = $db->fetch_assoc($motify_checklength);
if ($motify_length == 30) // seems like this key gives 30 days subscription
{
// let's check when does this user's current subscription end
$motify_checkcsgo = "SELECT `csgo` FROM `".$db->prefix."users` WHERE `id` = '".$pun_user['id'] or error('[Motify] Unable to check current subscription time', __FILE__, __LINE__, $db->error());
$db->query($motify_checkcsgo);
$motify_csgo = $db->fetch_assoc($motify_checkcsgo);
// this key is used since now
$db->query("UDPATE `".$db->prefix."keys` SET `used` = '1' WHERE `akey` = '".$motify_akey) or error('[Motify] Unable to deactivate key', __FILE__, __LINE__, $db->error());
// let's add those days to his current subscription
$new30 = date("Y-m-d H:i:s", $motify_csgo + (24*3600*30));
$db->query("UPDATE `".$db->prefix."users` SET `csgo` = '".$new30) or error('[Motify] Unable to update subscription', __FILE__, __LINE__, $db->error());
$db->query("UDPATE `".$db->prefix."users` SET `group_id` = '5' WHERE `id` = '".$pun_user['id']) or error('[Motify] Unable to update group', __FILE__, __LINE__, $db->error());
redirect("https://example.com/forums/", "Your subscription has been successfully activated! Redirecting...");
}
if ($motify_length == 90) // seems like this key gives 90 days subscription
{
// let's check when does this user's current subscription end
$motify_checkcsgo = "SELECT `csgo` FROM `".$db->prefix."users` WHERE `id` = '".$pun_user['id'] or error('[Motify] Unable to check current subscription time', __FILE__, __LINE__, $db->error());
$db->query($motify_checkcsgo);
$motify_csgo = $db->fetch_assoc($motify_checkcsgo);
// this key is used since now
$db->query("UDPATE `".$db->prefix."keys` SET `used` = '1' WHERE `akey` = '".$motify_akey) or error('[Motify] Unable to deactivate key', __FILE__, __LINE__, $db->error());
// let's add those days to his current subscription
$new90 = date("Y-m-d H:i:s", $motify_csgo + (24*3600*90));
$db->query("UPDATE `".$db->prefix."users` SET `csgo` = '".$new90) or error('[Motify] Unable to update subscription', __FILE__, __LINE__, $db->error());
$db->query("UDPATE `".$db->prefix."users` SET `group_id` = '5' WHERE `id` = '".$pun_user['id']) or error('[Motify] Unable to update group', __FILE__, __LINE__, $db->error());
redirect("https://example.com/forums/", "Your subscription has been successfully activated! Redirecting...");
}
if ($motify_length == 365) // seems like this key gives 1 year subscription
{
// let's check when does this user's current subscription end
$motify_checkcsgo = "SELECT `csgo` FROM `".$db->prefix."users` WHERE `id` = '".$pun_user['id'] or error('[Motify] Unable to check current subscription time', __FILE__, __LINE__, $db->error());
$db->query($motify_checkcsgo);
$motify_csgo = $db->fetch_assoc($motify_checkcsgo);
// this key is used since now
$db->query("UDPATE `".$db->prefix."keys` SET `used` = '1' WHERE `akey` = '".$motify_akey) or error('[Motify] Unable to deactivate key', __FILE__, __LINE__, $db->error());
$new365 = date("Y-m-d H:i:s", $motify_csgo + (24*3600*365));
// let's add those days to his current subscription
$db->query("UPDATE `".$db->prefix."users` SET `csgo` = '".$new365) or error('[Motify] Unable to update subscription', __FILE__, __LINE__, $db->error());
// upgrading user to premium group
$db->query("UDPATE `".$db->prefix."users` SET `group_id` = '5' WHERE `id` = '".$pun_user['id']) or error('[Motify] Unable to update group', __FILE__, __LINE__, $db->error());
redirect("https://example.com/forums/", "Your subscription has been successfully activated! Redirecting...");
}
}
}
// expired or no subscription at all
else
{
// this has same logic as above one but this user doesn't have active subscription already or it has been expired
$motify_akey = $db->escape($_POST['key']);
$motify_check_key = $db->query("SELECT COUNT(*) FROM `".$db->prefix."keys` WHERE `akey` = '".$motify_akey."' AND `used` = '0'") or error('[Motify] Unable to validate activation key', __FILE__, __LINE__, $db->error());
$db->query($motify_key_check);
$motify_key_result = $db->num_rows($motify_keycheck);
if ($motify_key_result > 0)
{
$motify_checklength = "SELECT `sub` FROM `".$db->prefix."keys` WHERE `akey` = '".$motify_akey or error('[Motify] Unable to subscription length', __FILE__, __LINE__, $db->error());
$db->query($motify_checklength);
$motify_length = $db->fetch_assoc($motify_checklength);
if ($motify_length == 30)
{
$db->query("UDPATE `".$db->prefix."keys` SET `used` = '1' WHERE `akey` = '".$motify_akey) or error('[Motify] Unable to deactivate key', __FILE__, __LINE__, $db->error());
$new30 = date("Y-m-d H:i:s", $motify_csgo (24*3600*30));
$db->query("UPDATE `".$db->prefix."users` SET `csgo` = '".$new30) or error('[Motify] Unable to update subscription', __FILE__, __LINE__, $db->error());
$db->query("UDPATE `".$db->prefix."users` SET `group_id` = '5' WHERE `id` = '".$pun_user['id']) or error('[Motify] Unable to update group', __FILE__, __LINE__, $db->error());
redirect("https://example.com/forums/", "Your subscription has been successfully activated! Redirecting...");
}
if ($motify_length == 90)
{
$db->query("UDPATE `".$db->prefix."keys` SET `used` = '1' WHERE `akey` = '".$motify_akey) or error('[Motify] Unable to deactivate key', __FILE__, __LINE__, $db->error());
$new90 = date("Y-m-d H:i:s", $motify_csgo (24*3600*90));
$db->query("UPDATE `".$db->prefix."users` SET `csgo` = '".$new90) or error('[Motify] Unable to update subscription', __FILE__, __LINE__, $db->error());
$db->query("UDPATE `".$db->prefix."users` SET `group_id` = '5' WHERE `id` = '".$pun_user['id']) or error('[Motify] Unable to update group', __FILE__, __LINE__, $db->error());
redirect("https://example.com/forums/", "Your subscription has been successfully activated! Redirecting...");
}
if ($motify_length == 365)
{
$db->query("UDPATE `".$db->prefix."keys` SET `used` = '1' WHERE `akey` = '".$motify_akey) or error('[Motify] Unable to deactivate key', __FILE__, __LINE__, $db->error());
$new365 = date("Y-m-d H:i:s", $motify_csgo (24*3600*365));
$db->query("UPDATE `".$db->prefix."users` SET `csgo` = '".$new365) or error('[Motify] Unable to update subscription', __FILE__, __LINE__, $db->error());
$db->query("UDPATE `".$db->prefix."users` SET `group_id` = '5' WHERE `id` = '".$pun_user['id']) or error('[Motify] Unable to update group', __FILE__, __LINE__, $db->error());
redirect("https://example.com/forums/", "Your subscription has been successfully activated! Redirecting...");
}
}
}
}
<!-- HTML part -->
<form method="POST">
<div class="inform">
<input type="hidden" name="form_sent" value="1">
<fieldset>
<div class="infldset">
<input type="text" maxlength="35" name="key" placeholder="Activation key" required>
<input type="submit" name="activate" value="Activate">
</div>
</fieldset>
</div>
</form>
keys
表格截图:
列的屏幕截图csgo
及其格式:
(如果用户从未有过有效订阅,则为 NULL)
编辑:我没有一个语法错误
解决方案
在这段代码中:
// check if activation key is valid and not used
$motify_check_key = $db->query("SELECT COUNT(*) FROM `".$db->prefix."keys` WHERE `akey` = '".$motify_akey."' AND `used` = '0'") or error('[Motify] Unable to validate activation key', __FILE__, __LINE__, $db->error());
$motify_key_result = $db->num_rows($motify_keycheck);
if ($motify_key_result > 0)
$motify_keycheck
你的倒数第二行来自哪里?不应该这样$motify_check_key
吗?但是,如果是,num_rows
则始终为 1,因为您要返回计数。因此,您想要检索 count 变量(因此在查询中使用 'as' 为其命名)并查看它的值。
当您的下一个查询返回到同一个表以获取不同的列时,您最好将两者结合起来。
在这一点:
$motify_length = $db->fetch_assoc($motify_checklength);
if ($motify_length == 30) // seems like this key gives 30 days subscription
$motify_length
是一个关联数组,所以它永远不会等于 30。你应该检查一下$motify_length['sub']
。这两个问题相同:
$motify_csgo = $db->fetch_assoc($motify_checkcsgo);
...
$new30 = date("Y-m-d H:i:s", $motify_csgo + (24*3600*30));
当您尝试对完整数组执行类似操作时,我看不出您如何不会出错。一些简单的调试使用var_dump
和echo
在这里会有所帮助。
推荐阅读
- python - Cannot increase accuracy of calculation (not enough decimal spaces)
- python-3.x - 从不和谐消息中提取不和谐用户 ID
- c# - c#避免多个MessageBox显示?
- spring-boot - 为 POST 的 HTTP 路径“http://localhost:8080/error”映射的模糊处理程序方法
- mongodb - 单个组的 Mongodb 查询限制
- php - 尽管设置了 max_execution_time,PHP 仍以 30 秒切割
- python - 从终端中正在进行的进程中读取命令行输出
- python - 重塑 Numpy 数组:“列表”对象不可调用
- compiler-construction - llvm IR 中的冗余基本块
- json - jasper 中的交叉表仅显示输入 json 中的第一列