Jump to content

another little help needed with queries.


seany123

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/159704-another-little-help-needed-with-queries/
Share on other sites

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'];

 

 

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));

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;

Woah, what happened to question marks? :D 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.

Woah, what happened to question marks? :D 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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.