thereaper87 Posted July 24, 2010 Share Posted July 24, 2010 Hello there, I have this snipped of code I have been trying to create since tuesday. So far there has been little success. What this does is, if the logged in user has 20,000,000 xp or more, it completes the mysql query which subtracts the 20,000,000. But if the user has less than 20,000,000 xp, it will echo "Do not have enough xp"....here is the code: <?php session_start(); include('../database.php'); $userid = $_SESSION['user']; $userxp = "SELECT xp FROM user WHERE username = '$userid'"; $amount = 19999999; if ($userxp > $amount){ $query = "UPDATE `user` SET `xp`=(`xp`-20000000) WHERE `username` = '$userid' LIMIT 1"; $result = mysql_query($query) or die('Query: ' . $query . '<br />Failed with: ' . mysql_error()); echo "You have purchased this item"; } else { echo "You do not have enough xp!"; } ?> If you need any additional info please ask! Right now I'm on an airplane so I may not be able to respond quickly. Quote Link to comment https://forums.phpfreaks.com/topic/208791-conditional-to-complete-mysql-query/ Share on other sites More sharing options...
Pikachu2000 Posted July 24, 2010 Share Posted July 24, 2010 What are you having trouble with? What is/isn't it doing? Quote Link to comment https://forums.phpfreaks.com/topic/208791-conditional-to-complete-mysql-query/#findComment-1090745 Share on other sites More sharing options...
awjudd Posted July 24, 2010 Share Posted July 24, 2010 You never actually run the first query against the database ... <?php session_start(); include('../database.php'); $userid = $_SESSION['user']; $userxp = mysql_fetch_array ( mysql_query ( "SELECT xp FROM user WHERE username = '$userid'" ) ); $amount = 19999999; if ($userxp [ 'xp' ] > $amount){ $query = "UPDATE `user` SET `xp`=(`xp`-20000000) WHERE `username` = '$userid' LIMIT 1"; $result = mysql_query($query) or die('Query: ' . $query . '<br />Failed with: ' . mysql_error()); echo "You have purchased this item"; } else { echo "You do not have enough xp!"; } ?> Would be a hacked out version of your code so that it should work. ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/208791-conditional-to-complete-mysql-query/#findComment-1090746 Share on other sites More sharing options...
thereaper87 Posted July 24, 2010 Author Share Posted July 24, 2010 So, the initial query has to be ran first before it can do the if statement? Quote Link to comment https://forums.phpfreaks.com/topic/208791-conditional-to-complete-mysql-query/#findComment-1090750 Share on other sites More sharing options...
thereaper87 Posted July 24, 2010 Author Share Posted July 24, 2010 Also, it worked! Thanks a mil for your help! Quote Link to comment https://forums.phpfreaks.com/topic/208791-conditional-to-complete-mysql-query/#findComment-1090752 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.