Jiraiya Posted April 1, 2009 Share Posted April 1, 2009 I looked online for tutorials on how to do this but i still don't understand im trying to use a if statement that if($row['gold'] >= 100) then it subtracts 100 from row gold and then adds 1 to row "potions" if anyone could help me or send a tutorial my way i would be very grateful. Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/ Share on other sites More sharing options...
trq Posted April 1, 2009 Share Posted April 1, 2009 I assume you want to update a table with this info? if ($row['gold'] >= 100) { $sql = "UPDATE tbl SET gold=gold-100, potions=potions+1 WHERE id = " . $row['id'] // execute query } Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798525 Share on other sites More sharing options...
Jiraiya Posted April 1, 2009 Author Share Posted April 1, 2009 yes thanks Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798526 Share on other sites More sharing options...
Jiraiya Posted April 1, 2009 Author Share Posted April 1, 2009 is there something wrong with the code it keeps coming up blank on my webpage. <?php $sql = mysql_query("SELECT * FROM users WHERE username = '$username'"); $row = mysql_fetch_array($sql); if ($row['gold'] >= 100) { $sql = "UPDATE users SET gold=gold-100, potions=potions+1 WHERE username = '$username'") or die(mysql_error()); echo '<center>You have bought one health potion</center>'; }else{ echo '<center>You don't have enough gold. Come back when you get more.</center>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798532 Share on other sites More sharing options...
lonewolf217 Posted April 1, 2009 Share Posted April 1, 2009 look at the color coding of the code you posted. the problem is this line echo '<center>You don't have enough gold. Come back when you get more.</center>; Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798534 Share on other sites More sharing options...
Jiraiya Posted April 1, 2009 Author Share Posted April 1, 2009 i still dont see what is wrong with that line Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798535 Share on other sites More sharing options...
trq Posted April 1, 2009 Share Posted April 1, 2009 echo "<center>You don't have enough gold. Come back when you get more.</center>"; Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798538 Share on other sites More sharing options...
Jiraiya Posted April 1, 2009 Author Share Posted April 1, 2009 i checked that and it still shows up white on the web browers <?php $sql = mysql_query("SELECT * FROM users WHERE username = '$username'"); $row = mysql_fetch_array($sql); if ($row['gold'] >= 100) { $sql = "UPDATE users SET gold=gold-100, potions=potions+1 WHERE username = '$username'") or die(mysql_error(); echo "<center>You have bought one health potion.</center>"; }else{ echo "<center>You don't have enough gold. Come back when you getmore.</center>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798628 Share on other sites More sharing options...
lonewolf217 Posted April 1, 2009 Share Posted April 1, 2009 missing a closing ) on your die() statement here <?php $sql = "UPDATE users SET gold=gold-100, potions=potions+1 WHERE username = '$username'") or die(mysql_error()); ?> turn on error reporting and you will see these errors Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798666 Share on other sites More sharing options...
Jiraiya Posted April 1, 2009 Author Share Posted April 1, 2009 this is the entire code so far i still didnt find any errors <?php mysql_connect("database", "username", "password") or die(mysql_error()); mysql_select_db("members") or die(mysql_error()); $sql = mysql_query("SELECT * FROM users WHERE username = '$username'"); $row = mysql_fetch_array($sql); if ($row['gold'] >= 100) { $sql = "UPDATE users SET gold = gold-100, potions = potions+1 WHERE username = '$username'") or die(mysql_error()); echo "<center>You have bought one health potion.</center>"; }else{ echo "<center>You don't have enough gold. Come back when you getmore.</center>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798717 Share on other sites More sharing options...
lonewolf217 Posted April 1, 2009 Share Posted April 1, 2009 turn on error reporting in your PHP.ini so you can catch syntax errors. you also have an extra parenthesis on your $sql line. plus, you cannot run "or die()" when you are only assigning a text string. that is used when you actually run the query <?php if ($row['gold'] >= 100) { $sql = "UPDATE users SET gold = gold-100, potions = potions+1 WHERE username = '$username'"; $result = mysql_query($sql) or die(mysql_error()); echo "<center>You have bought one health potion.</center>"; }else{ echo "<center>You don't have enough gold. Come back when you getmore.</center>"; } again, turn on error reporting to catch these yourself Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798795 Share on other sites More sharing options...
manny Posted April 1, 2009 Share Posted April 1, 2009 i bellieve this should do it <?php mysql_connect("database", "username", "password") or die(mysql_error()); mysql_select_db("members") or die(mysql_error()); $sql = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); $row = mysql_fetch_array($sql); if ($row['gold'] >= 100) { ////Had error in this line //// $sql = ("UPDATE users SET gold = gold-100, potions = potions+1 WHERE username = '$username'") or die(mysql_error()); $sql = ("UPDATE users SET `gold` = `gold`-100, `potions` = `potions`+1 WHERE `username` = '$username'") or die(mysql_error()); echo "<center>You have bought one health potion.</center>"; }else{ echo "<center>You don't have enough gold. Come back when you getmore.</center>"; } ?> Let us know Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798813 Share on other sites More sharing options...
Jiraiya Posted April 1, 2009 Author Share Posted April 1, 2009 this code works better in the sense that now the web page shows up but the script still isnt doing anything it keeps displaying "You don't have enough gold. Come back when you getmore. Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798833 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 Do they have enough gold? I would print out the $row['gold'] in the else: $currentGold = $row['gold'] - 100; echo "<center>You have bought one health potion. You now have {$currentGold}.</center>"; }else{ echo "<center>You don't have enough gold, you currently have {$row['gold']}. Come back when you getmore.</center>"; } And see what is displayed. Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798838 Share on other sites More sharing options...
Jiraiya Posted April 1, 2009 Author Share Posted April 1, 2009 it displayed "You don't have enough gold, you currently have {$row['gold']}. Come back when you getmore." Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798844 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 Hmm. Not sure why it would have returned that...maybe it is an older version of PHP I do not know. This this instead: $currentGold = $row['gold'] - 100; echo "<center>You have bought one health potion. You now have " . $currentGold . ".</center>"; }else{ echo "<center>You don't have enough gold, you currently have " . $row['gold'] . ". Come back when you getmore.</center>"; } See if that produces a number. I have a hunch that it showed the $row['gold'] portion maybe because it is not defined...no clue....it seems there maybe an issue on your server ??? Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798847 Share on other sites More sharing options...
Jiraiya Posted April 1, 2009 Author Share Posted April 1, 2009 You don't have enough gold, you currently have . Come back when you getmore. thats what is displayed Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798851 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 <?php mysql_connect("database", "username", "password") or die(mysql_error()); mysql_select_db("members") or die(mysql_error()); $sql = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); if (mysql_num_rows($sql) > 0) { $row = mysql_fetch_array($sql); if ($row['gold'] >= 100) { ////Had error in this line //// $sql = ("UPDATE users SET gold = gold-100, potions = potions+1 WHERE username = '$username'") or die(mysql_error()); $sql = ("UPDATE users SET `gold` = `gold`-100, `potions` = `potions`+1 WHERE `username` = '$username'") or die(mysql_error()); echo "<center>You have bought one health potion.</center>"; }else{ echo "<center>You don't have enough gold. Come back when you getmore.</center>"; } }else { echo "No rows were returned by the SQL. Username entered was " . $username; } ?> Where does $username come from? Try the above, my bet is that $username will not display. You need to assign $username a value some how whether from $_POST/$_GET/$_SESSION, it seems like you are assuming register_globals is turned on, which it is not and this is causing the problem. Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798856 Share on other sites More sharing options...
Jiraiya Posted April 1, 2009 Author Share Posted April 1, 2009 No rows were returned by the SQL. Username entered was is what it displayed Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798857 Share on other sites More sharing options...
Jiraiya Posted April 1, 2009 Author Share Posted April 1, 2009 ok it now displays "You have bought one health potion." but its not updating the database $sql = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); $row = mysql_fetch_array($sql); if ($row['gold'] >= 100) { $sql = ("UPDATE users SET `gold` = `gold`-100, `potions` = `potions`+ 1 WHERE `username` = '$username'") or die(mysql_error()); echo "<center>You have bought one health potion.</center>"; }else{ echo "<center>You don't have enough gold. Come back when you getmore.</center>"; } Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798871 Share on other sites More sharing options...
manny Posted April 1, 2009 Share Posted April 1, 2009 Do they have enough gold? I would print out the $row['gold'] in the else: $currentGold = $row['gold'] - 100; echo "<center>You have bought one health potion. You now have {$currentGold}.</center>"; }else{ echo "<center>You don't have enough gold, you currently have {$row['gold']}. Come back when you getmore.</center>"; } And see what is displayed. correction on echo "<center>You don't have enough gold, you currently have ". $row['gold'].". Come back when you get more.</center>"; goahead and see if it shows or what it shows Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798873 Share on other sites More sharing options...
Jiraiya Posted April 1, 2009 Author Share Posted April 1, 2009 ok it now displays "You have bought one health potion." but its not updating the database Code: [select] $sql = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); $row = mysql_fetch_array($sql); if ($row['gold'] >= 100) { $sql = ("UPDATE users SET `gold` = `gold`-100, `potions` = `potions`+ 1 WHERE `username` = '$username'") or die(mysql_error()); echo "<center>You have bought one health potion.</center>"; }else{ echo "<center>You don't have enough gold. Come back when you getmore.</center>"; } Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798876 Share on other sites More sharing options...
manny Posted April 1, 2009 Share Posted April 1, 2009 wow i totally forgot change $sql = ("UPDATE users SET `gold` = `gold`-100, `potions` = `potions`+ 1 WHERE `username` = '$username'") or die(mysql_error()); to change $sql = ("UPDATE users SET `gold` = (`gold`-100), `potions` = (`potions`+ 1) WHERE `username` = '$username'") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798896 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 $sql = mysql_query("UPDATE users SET `gold` = `gold`-100, `potions` = `potions`+ 1 WHERE `username` = '$username'") or die(mysql_error()); Will get it to update the database. Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798902 Share on other sites More sharing options...
manny Posted April 1, 2009 Share Posted April 1, 2009 True but the other way will let you run more complex formulas better to get use to a better way Finally SOLVED Quote Link to comment https://forums.phpfreaks.com/topic/152063-solved-phpmath-help-please/#findComment-798913 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.