seany123 Posted May 26, 2009 Share Posted May 26, 2009 i have this code... <?php $query = $db->execute("update `gangs` set `money`=? where `id`=?", array($gang['money'] + $_POST['money'], $player->gang_id)); $query1 = $db->execute("update `players` set `money`=? where `id`=?", array($player->money - $_POST['money'], $player->id)); ?> $query1 works fine removing the correct amount but... $query: instead of it adding $gang['money'] and $_POST['money'] it just sets $gang['money'] to whatever the value $_POST['money'] is... so say $gang['money'] = 300 and $_POST['money'] = 50, it should set the new $gang['money'] to 350... instead it just sets it to 50 Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/ Share on other sites More sharing options...
gevans Posted May 26, 2009 Share Posted May 26, 2009 print $gang['money'] are yu getting what you expect? Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842335 Share on other sites More sharing options...
seany123 Posted May 26, 2009 Author Share Posted May 26, 2009 i already have both echo'd.. im getting what i expect before the query is executed, but not after... Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842337 Share on other sites More sharing options...
gevans Posted May 26, 2009 Share Posted May 26, 2009 try pulling the adding outside of the query method call $foo = var1 + var 2 yourmethod(array($foo, others)) Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842338 Share on other sites More sharing options...
Ken2k7 Posted May 26, 2009 Share Posted May 26, 2009 Have you tried echo $gang['money'] + $_POST['money']; before the query? Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842339 Share on other sites More sharing options...
seany123 Posted May 26, 2009 Author Share Posted May 26, 2009 Have you tried echo $gang['money'] + $_POST['money']; before the query? that seems to only echo the $_POST['money']. try pulling the adding outside of the query method call $foo = var1 + var 2 yourmethod(array($foo, others)) what like this? $gang['money'] = $gang['money'] + $_POST['money']; Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842343 Share on other sites More sharing options...
Ken2k7 Posted May 26, 2009 Share Posted May 26, 2009 var_dump($gang['money']); Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842344 Share on other sites More sharing options...
seany123 Posted May 26, 2009 Author Share Posted May 26, 2009 var_dump($gang['money']); it just echo's NULL Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842347 Share on other sites More sharing options...
dennismonsewicz Posted May 26, 2009 Share Posted May 26, 2009 how are you getting $gang['money']? Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842350 Share on other sites More sharing options...
kickstart Posted May 26, 2009 Share Posted May 26, 2009 Hi Suggests that the $gang[] array hasn't been set up. Could even just be a typo that you have set up the array as $Gang[]. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842352 Share on other sites More sharing options...
Ken2k7 Posted May 26, 2009 Share Posted May 26, 2009 Or did you mean $gang->money? If the money you're updating is the gang itself (assuming id is a PK), then try this $query = $db->execute("update `gangs` set `money`= `money` + ? where `id`=?", array($_POST['money'], $player->gang_id)); Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842358 Share on other sites More sharing options...
Philip Posted May 26, 2009 Share Posted May 26, 2009 Instead of using defined variables somewhere else in the script, probably from another query, why not just do something like: $money = (int) $_POST['money']; // Set for int, not really needed with prepared statements but good habits $query = $db->execute("update `gangs` set `money`= `money` + ? where `id`=?", array($money, $player->gang_id)); $query1 = $db->execute("update `players` set `money`= `money` - ? where `id`=?", array($money, $player->id)); Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842360 Share on other sites More sharing options...
seany123 Posted May 26, 2009 Author Share Posted May 26, 2009 Or did you mean $gang->money? If the money you're updating is the gang itself (assuming id is a PK), then try this $query = $db->execute("update `gangs` set `money`= `money` + ? where `id`=?", array($_POST['money'], $player->gang_id)); that has made the query work... but it still means i cant do other things with the values like this... <?php if(($gang['money'] + $_POST['money']) >= $gang['maxmoney']) { } ?> and the values are all pulled using this query at the top of my page... $query = $db->execute("select * from gangs where `id`= $player->gang_id"); $gang = $query->fetchrow; Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842388 Share on other sites More sharing options...
Ken2k7 Posted May 26, 2009 Share Posted May 26, 2009 Woah, what happened to question marks? I really hate the question mark style.... I can't tell if your SQL works or not. I assume it doesn't. $query = $db->execute("select * from gangs where `id`= ?", array($player->gang_id)); Alternatively, you could put brackes "{}" around $player->gang_id. Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842398 Share on other sites More sharing options...
seany123 Posted May 26, 2009 Author Share Posted May 26, 2009 Woah, what happened to question marks? I really hate the question mark style.... I can't tell if your SQL works or not. I assume it doesn't. $query = $db->execute("select * from gangs where `id`= ?", array($player->gang_id)); Alternatively, you could put brackes "{}" around $player->gang_id. well i dont really know.. because when i echo things like.. <?= $english_format_number = number_format($gang['maxmoney']);?> it returns the correct value so for that reason i thought the query was working... its just when puting the values into an if statement it doesnt seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/#findComment-842425 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.