![](/img/trans.png)
[英]Simple PHP Login Form Returns '0' mysqli_num_rows. What's wrong with my code logic?
[英]What is wrong with my logic? Trying to make a simple mysqli script
我正在嘗試為我的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_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
一些簡單的調試會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.