IFs and $_REQUESTs not working?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

IFs and $_REQUESTs not working?

Post by cbrian »

I can't figure out what's wrong with this. I've been looking at it for a couple hours, but nothing I try fixes it. If I go to game.php?do=sell then nothing happens. Nothing except what happens everytime you go to the page, which is not included in this.

Code: Select all

<?php
if($_REQUEST['do'] == "buy") {
$buy = $_REQUEST['buy'];
if($_REQUEST['what'] == "weapon") {
$gold = mysql_query("SELECT gold FROM users WHERE userid='$userid'");
$gold = mysql_fetch_array($gold);
$gold = $gold[0];

$weapon = mysql_query("SELECT weapon FROM users WHERE userid='$userid'");
$weapon = mysql_fetch_array($weapon);
$weapon = $weapon[0];

if($weapon > 0) {
print("You already have a weapon! You need to sell it before buying another one!");
} else {
$wep = mysql_query("SELECT * FROM weapons WHERE `id`='$buy'");
$wep = mysql_fetch_assoc($wep);
if($gold >= $wep['price']) {
mysql_query("UPDATE users SET gold=(gold -  {$wep['price']}) WHERE userid='$userid'");
mysql_query("UPDATE users SET weapon='$buy' WHERE userid='$userid'");
echo "You bought the " . $wep['name'];
} else { 
echo "You don't have enough gold to buy that!"; 
}
}
} elseif($_REQUEST['what'] == "armor") {

$gold = mysql_query("SELECT gold FROM users WHERE userid='$userid'");
$gold = mysql_fetch_array($gold);
$gold = $gold[0];

$armor = mysql_query("SELECT armor FROM users WHERE userid='$userid'");
$armor = mysql_fetch_array($armor);
$armor = $armor[0];

if($armor > 0) {
print("You already have an armor! You need to sell it before buying another one!");
} else {
$arm = mysql_query("SELECT * FROM armor WHERE `id`='$buy'");
$arm = mysql_fetch_assoc($arm);
if($gold >= $arm['price']) {
mysql_query("UPDATE users SET gold=(gold -  {$arm['price']}) WHERE userid='$userid'");
mysql_query("UPDATE users SET armor='$buy' WHERE userid='$userid'");
echo "You bought the " . $arm['name'];
} else { echo "You don't have enough gold to buy that!"; }
}


if($_REQUEST['do'] == "sell") {
echo "test";
if($_REQUEST['what'] == "weapon") {
$weapon = mysql_query("SELECT weapon FROM users WHERE userid='$userid'");
$weapon = mysql_fetch_array($weapon);
$weapon = $weapon[0];

$gold = mysql_query("SELECT gold FROM users WHERE userid='$userid'");
$gold = mysql_fetch_array($gold);
$gold = $gold[0];

if($weapon == $_REQUEST['sell']) {
$weapon = mysql_query("SELECT weapon FROM users WHERE userid='$userid'");
$weapon = mysql_fetch_array($weapon);
$weapon = $weapon[0];
$update = mysql_query("UPDATE users SET weapon = '0' WHERE userid='$userid'");
$sellfor = mysql_query("SELECT price FROM weapons WHERE id='$weapon'");
$sellfor = mysql_fetch_array($sellfor);
$sellfor = $sellfor[0] / 2;
$query = mysql_query("UPDATE users SET gold=(gold + $sellfor) WHERE userid='$userid'");
print("You have sold your weapon for $sellfor g.");
} else {
print("You don't have a weapon to sell!");
}
}
if($_REQUEST['what'] == "armor") {
$armor = mysql_query("SELECT armor FROM users WHERE userid='$userid'");
$armor = mysql_fetch_array($armor);
$armor = $armor[0];
mysql_query("UPDATE users SET armor='0' WHERE userid='$userid'");
$sellfor = mysql_query("SELECT price FROM armor WHERE id='$armor'");
$sellfor = mysql_fetch_array($sellfor);
$sellfor = $sellfor[0] / 2;
$query = mysql_query("UPDATE users SET gold=(gold + $sellfor) WHERE userid='$userid'");
print("You have sold your armor for $sellfor g.");
} else {
print("You don't have an armor to sell!");
}

}
}
?>
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Can you be more specific about your problem.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

the script won't work anyway....

make sure you get the { and } at the right places... a good editor will assist you with this (fe: with vim you can find the matching brace by hitting the % button)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

timvw wrote:(fe: with vim you can find the matching brace by hitting the % button)
Off topic but - Crimson Editor does this as soon as you type a bracket (it underlines matching pairs).
Ok I guess we'd better keep it on topic...
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

Timvw, why won't it work anyways?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

according to your code:

Code: Select all

if($_REQUEST['do'] == "buy") {
there is no matching }


And the if($_REQUEST['do'] == "sell") is in the part where $_REQUEST['do'] equals "buy". Thus it will never be executed.

(But i'm on serious drugs, so it's possible i'm not thinking clear)
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

That's what I thought at first - but when I added a '}' it gave me a parse error and said it was unexpected.
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Just curious are you making a php text based game? Anything like http://www.kingsofchaos.com or http://www.mediwar.com?
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

Yes, this is called Kingdom of Dragons.
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Post by Phoenixheart »

Code: Select all

if($_REQUEST['do'] == "buy")
{
        $buy = $_REQUEST['buy'];
        if($_REQUEST['what'] == "weapon") {
                $gold = mysql_query("SELECT gold FROM users WHERE userid='$userid'");
                $gold = mysql_fetch_array($gold);
                $gold = $gold[0];
                $weapon = mysql_query("SELECT weapon FROM users WHERE userid='$userid'");
                $weapon = mysql_fetch_array($weapon);
                $weapon = $weapon[0];
                if($weapon > 0) {
                        print("You already have a weapon! You need to sell it before buying another one!");
                } else
                {
                        $wep = mysql_query("SELECT * FROM weapons WHERE `id`='$buy'");
                        $wep = mysql_fetch_assoc($wep);
                        if($gold >= $wep['price']) {
                                mysql_query("UPDATE users SET gold=(gold -  {$wep['price']}) WHERE userid='$userid'");
                                mysql_query("UPDATE users SET weapon='$buy' WHERE userid='$userid'");
                                echo "You bought the " . $wep['name'];
                        } else {
                               echo "You don't have enough gold to buy that!";
                        }
                }
        }
        elseif($_REQUEST['what'] == "armor") {
                $gold = mysql_query("SELECT gold FROM users WHERE userid='$userid'");
                $gold = mysql_fetch_array($gold);
                $gold = $gold[0];
                $armor = mysql_query("SELECT armor FROM users WHERE userid='$userid'");
                $armor = mysql_fetch_array($armor);
                $armor = $armor[0];
                if($armor > 0) {
                        print("You already have an armor! You need to sell it before buying another one!");
                }
                else {
                        $arm = mysql_query("SELECT * FROM armor WHERE `id`='$buy'");
                        $arm = mysql_fetch_assoc($arm);
                        if($gold >= $arm['price']) {
                                mysql_query("UPDATE users SET gold=(gold -  {$arm['price']}) WHERE userid='$userid'");
                                mysql_query("UPDATE users SET armor='$buy' WHERE userid='$userid'");
                                echo "You bought the " . $arm['name'];} else { echo "You don't have enough gold to buy that!";
                        }
                }
                if($_REQUEST['do'] == "sell") {
                        echo "test";
                        if($_REQUEST['what'] == "weapon") {
                                $weapon = mysql_query("SELECT weapon FROM users WHERE userid='$userid'");
                                $weapon = mysql_fetch_array($weapon);
                                $weapon = $weapon[0];
                                $gold = mysql_query("SELECT gold FROM users WHERE userid='$userid'");
                                $gold = mysql_fetch_array($gold);
                                $gold = $gold[0];
                                if($weapon == $_REQUEST['sell']) {
                                        $weapon = mysql_query("SELECT weapon FROM users WHERE userid='$userid'");
                                        $weapon = mysql_fetch_array($weapon);
                                        $weapon = $weapon[0];
                                        $update = mysql_query("UPDATE users SET weapon = '0' WHERE userid='$userid'");
                                        $sellfor = mysql_query("SELECT price FROM weapons WHERE id='$weapon'");
                                        $sellfor = mysql_fetch_array($sellfor);
                                        $sellfor = $sellfor[0] / 2;
                                        $query = mysql_query("UPDATE users SET gold=(gold + $sellfor) WHERE userid='$userid'");
                                        print("You have sold your weapon for $sellfor g.");
                                }
                                else {
                                        print("You don't have a weapon to sell!");
                                }
                        }
                        if($_REQUEST['what'] == "armor") {
                                $armor = mysql_query("SELECT armor FROM users WHERE userid='$userid'");
                                $armor = mysql_fetch_array($armor);
                                $armor = $armor[0];
                                mysql_query("UPDATE users SET armor='0' WHERE userid='$userid'");
                                $sellfor = mysql_query("SELECT price FROM armor WHERE id='$armor'");
                                $sellfor = mysql_fetch_array($sellfor);
                                $sellfor = $sellfor[0] / 2;
                                $query = mysql_query("UPDATE users SET gold=(gold + $sellfor) WHERE userid='$userid'");
                                print("You have sold your armor for $sellfor g.");
                        } else {
                                print("You don't have an armor to sell!");
                        }
                 }
          }
Above is the code "re-formatted" for an easier view. See, you don't have a close "}" for the first "if" statement. I think that's the cause.
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

When I add the }, it gives me

Parse error: parse error, unexpected '}' in /home/stlawren/www/cbc/KoD/game/blacksmith.php on line 99
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Post by Phoenixheart »

WHERE to add a "}" does matter, man :wink:
If possible, you should post ALL of your code here, so everyone can run it for a test. May be that "line 99" error can be solved.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

currently the flow looks like this:

Code: Select all

if($_REQUEST['do'] == "buy")
{
        ....    
        elseif($_REQUEST['what'] == "armor") {
                 if($_REQUEST['do'] == "sell") {
                 ....
}
please notice that it's impossible that do equals sell (because it already equals buy)
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

I got it fixed. I had to take away a }
Post Reply