I am having trouble with the following code:
$usrlvlup = mysql_query("select user_statlevel from phpbb_users where username='$username'");
$usrlvlup = mysql_fetch_array($usrlvlup);
$usrlvlup = $usrlvlup[0] + 1;
$xpneeded = mysql_query("select reqxp from phpbb_user_levels where level='$usrlvlup'");
$xpneeded = mysql_fetch_array($xpneeded);
$xpneeded = $xpneeded[0];
$new_exp = $userdata['user_exp']+$gainexp;
$new_lvl_exp = $new_exp;
//get level gains for user's class
$classsql = "select statgains from phpbb_classes where class='$userdata[user_class]'";
if ( !($classresult = $db->sql_query($classsql)) ) { message_die(GENERAL_MESSAGE, 'Fatal Error!'); }
$classrow = $db->sql_fetchrow($classresult);
$stat_gains = explode(',',$classrow['statgains']);
//level gains
$hpgain = rand($stat_gains[0],$stat_gains[1]); $mpgain = rand($stat_gains[2],$stat_gains[3]); $attgain = rand($stat_gains[4],$stat_gains[5]); $defgain = rand($stat_gains[6],$stat_gains[7]);
//if they've gained a level, update level, hp, mp, att, def & experience, if not, just update experience.
$islevelup = ($new_exp >= $xpneeded) ? 'yes' : 'no';
$levelup = ($new_exp >= $xpneeded) ? ', user_statlevel = user_statlevel + 1, user_hpmax = user_hpmax + '.$hpgain.', user_mpmax = user_mpmax + '.$mpgain.', user_att = user_att + '.$attgain.', user_def = user_def + '.$defgain.', user_exp = '.$new_lvl_exp.' ' : ', user_exp = '.$new_exp. ' ';
//create message to be displayed after posting!
$levelupmessage = ($islevelup == 'yes') ? "<br /><b>You've gone up a level!</b><br />Hit Points increased by ".$hpgain."<br />Magic Points increased by ".$mpgain.".<br />Attack increased by ".$attgain."<br />Defence increased by ".$defgain.".<p>" : "";
$sql = "UPDATE " . USERS_TABLE . " SET" .
substr($levelup,1) .
"WHERE user_id = $userdata[user_id]";
The point is to check the user's level, and see what the required xp for the next level is. If it is less than or equal to the experience they would have now, then increase their level and update stats etc. Something weird is happening though, every post users make (which gives them experience) gives them a lvl up, practically ignoring the requirements. Any ideas why this is happening? Probably something simple.
If it helps any, here is the unmodified code, which uses a static number to get the limit. This works fine.
//levelup check
$poster_expmax = $stats_config[0];
$new_exp = $userdata['user_exp']+$gainexp;
$new_lvl_exp = $new_exp - $poster_expmax;
//get level gains for user's class
$classsql = "select statgains from phpbb_classes where class='$userdata[user_class]'";
if ( !($classresult = $db->sql_query($classsql)) ) { message_die(GENERAL_MESSAGE, 'Fatal Error!'); }
$classrow = $db->sql_fetchrow($classresult);
$stat_gains = explode(',',$classrow['statgains']);
//level gains
$hpgain = rand($stat_gains[0],$stat_gains[1]); $mpgain = rand($stat_gains[2],$stat_gains[3]); $attgain = rand($stat_gains[4],$stat_gains[5]); $defgain = rand($stat_gains[6],$stat_gains[7]);
//if they've gained a level, update level, hp, mp, att, def & experience, if not, just update experience.
$islevelup = ($new_exp >= $poster_expmax) ? 'yes' : 'no';
$levelup = ($new_exp >= $poster_expmax) ? ', user_statlevel = user_statlevel + 1, user_hpmax = user_hpmax + '.$hpgain.', user_mpmax = user_mpmax + '.$mpgain.', user_att = user_att + '.$attgain.', user_def = user_def + '.$defgain.', user_exp = '.$new_lvl_exp.' ' : ', user_exp = '.$new_exp. ' ';
//create message to be displayed after posting!
$levelupmessage = ($islevelup == 'yes') ? "<br /><b>You've gone up a level!</b><br />Hit Points increased by ".$hpgain."<br />Magic Points increased by ".$mpgain.".<br />Attack increased by ".$attgain."<br />Defence increased by ".$defgain.".<p>" : "";
$sql = "UPDATE " . USERS_TABLE . " SET" .
substr($levelup,1) .
"WHERE user_id = $userdata[user_id]";