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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.