Page 1 of 1

IFs and $_REQUESTs not working?

Posted: Thu Apr 07, 2005 8:31 pm
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!");
}

}
}
?>

Posted: Thu Apr 07, 2005 10:03 pm
by neophyte
Can you be more specific about your problem.

Posted: Fri Apr 08, 2005 6:37 am
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)

Posted: Fri Apr 08, 2005 6:41 am
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...

Posted: Fri Apr 08, 2005 4:55 pm
by cbrian
Timvw, why won't it work anyways?

Posted: Fri Apr 08, 2005 5:51 pm
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)

Posted: Fri Apr 08, 2005 6:24 pm
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.

Posted: Fri Apr 08, 2005 6:33 pm
by anthony88guy
Just curious are you making a php text based game? Anything like http://www.kingsofchaos.com or http://www.mediwar.com?

Posted: Fri Apr 08, 2005 6:43 pm
by cbrian
Yes, this is called Kingdom of Dragons.

Posted: Sat Apr 09, 2005 6:12 am
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.

Posted: Sat Apr 09, 2005 7:32 am
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

Posted: Mon Apr 11, 2005 4:25 am
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.

Posted: Mon Apr 11, 2005 10:51 am
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)

Posted: Mon Apr 11, 2005 4:13 pm
by cbrian
I got it fixed. I had to take away a }