Variables in if() statements

I’ve been working on a PHP game, and it’s doing alright so far. You can choose from one of 4 types of people. They each have different stats.

I added their stats using if() statements. Take a look. The first section is just the form, the next is the actual code. I took out my database username and password, so don’t say that’s the problem.

[code]<?php
session_start();
if(isset($_SESSION[‘email’])) {

}

$con = mysql_connect(‘localhost’,‘bklyn’);
if(!$con) {
die('Error connecting to localhost: ’ . mysql_error());
}
$db = mysql_select_db(‘socialdb’,$con);
if(!$db) {
die('Error connecting to database: ’ . mysql_error());
}
?>

New Hero Hero name:
Knight Mage Assassin Barbarian
[/code]

And the PHP…

[php]<?php
session_start();
if(isset($_SESSION[‘email’])) {

}

$name = $_POST[‘name’];
$class = $_POST[‘class’];

$email = $_SESSION[‘email’];

$one = “SELECT * FROM rpg WHERE email=’$email’ AND class=’$class’”;
$two = mysql_query($one);

if($two == 2) {
echo “You already have two heroes of this class. Go <a href=“newhero.php”>back to make a new hero.”;
}

if($class == “knight”) {
$strength = 10;
$specialstrength = 5;
$defense = 9;
$specialdefense = 7;
$accuracy = 8;
$move1 = “Swing your sword”;
$move2 = “Bang heads (your armor is so tough, you take no damage)”;
$move3 = “Poison or burn enemy (40 energy)”;
}
if($class == “mage”) {
$strength = 5;
$specialstrength = 15;
$defense = 7;
$specialdefense = 10;
$accuracy = 8;
$move1 = “Bash enemy with wand”;
$move2 = “Dangerous gust of wind (30 energy)”;
$move3 = “Small fireball, 10% chance of burn (25 energy)”;
}
if($class == “assassin”) {
$strength = 15;
$specialstrength = 5;
$defense = 8;
$specialdefense = 5;
$accuracy = 20;
$move1 = “Tackle”;
$move2 = “Swing secret sword”;
$move3 = “Restore health (45 energy)”;
}
if($class == “barbarian”) {
$strength = 18;
$specialstrength = 10;
$defense = 5;
$specialdefense = 5;
$accuracy = 8;
$move1 = “Crazily swing arms”;
$move2 = “Bite”;
$move3 = “Burp (75% chance of poison) (30 energy)”;
}

$sql = “INSERT INTO rpg (Name, Level, XP, Health, Email, Gold, Energy, Class, Strength, SpecialStrength, Defense, SpecialDefense, Accuracy, Move1, Move2, Move3) VALUES ($name, 1, 0, 100, $email, 1000, 50, $class, $strength, $specialstrength, $defense, $specialdefense, $accuracy, $move1, $move2, $move3)”;

if(!$sql) {
die(mysql_error());
}

else {
echo “Hero successfully created. <a href=“heroes.php”>Click here to return to the hero page.”;
}

?>[/php]

The error is saying that all of the variables holding stats ($strength, $defense, $accuracy, so on) are undefined. I clearly defined them. Help? Thank you!

I got it working. I changed the if() statements to if, elseif, another elseif, and an else (the plain else having no expression, of course)

Also, I had to change my INSERT statement. I put apostrophes around the values and it worked! Can someone take this down please?

Whoops, a new problem came up. Whenever I sign as a person like, say an Assassin, it gives the stats of a Barbarian. This is because Barbarian is the last of the if/elseif/else series. How do I prevent this?

Its hard to say without seeing your code but if it is not findining the right class then it does not == the right one maybe you need to trim the var first to get rid of any white space

What do you mean?

I posted my code…

I think i was looking at another one with no code, any how what does the $class var echo out as ?
Does it have white space ?

like I said trim it see if that helps.

Its hard to see a error code you did not tell us what the class var is set as is it assigned the assassin string or not ?

I’ll just start a new example.

[php]$custom = 3;

if($custom == 3) {
$custom2 = 5;
}

elseif($custom == 4) {
$custom2 = 10;
}[/php]

If I was to do this (I never tested it, but I based this on results from my code in the original post) then $custom2 would equal 10. I wanted it to equal 5 (but only if $custom equals 3)

How can I fix this?

You said you have changed your code so after you redone your code you had trouble.
So I said without seeing your NEW code its hard to find what you have done wrong or need help with.

So post your updated code.

I have no idea what you want now because you where talking about assassin not being asigned to the $class var now you have gone off track and going on about numbers which I have no idea what you are trying to do.

But it looks like you have answered your own question to.

What I said before If you look at the VAR so custom make sure it has no whitespace

If it has white space it will not == any of your results it will then drop the to last so
[php]trim($custom)[/php]

This is the updated code, near identical to the previous code. (The form code is the same, because nothing changed)

[php]<?php
session_start();
if(isset($_SESSION[‘email’])) {

}

$name = $_POST[‘name’];
$class = $_POST[‘class’];

$email = $_SESSION[‘email’];

$one = “SELECT * FROM rpg WHERE email=’$email’ AND class=’$class’”;
$two = mysql_query($one);

if($two == 2) {
echo “You already have two heroes of this class. Go <a href=“newhero.php”>back to make a new hero.”;
}

if($class == “knight”) {
$strength = 10;
$specialstrength = 5;
$defense = 9;
$specialdefense = 7;
$accuracy = 8;
$move1 = “Swing your sword”;
$move2 = “Bang heads (your armor is so tough, you take no damage)”;
$move3 = “Poison or burn enemy (40 energy)”;
}
elseif($class == “mage”) {
$strength = 5;
$specialstrength = 15;
$defense = 7;
$specialdefense = 10;
$accuracy = 8;
$move1 = “Bash enemy with wand”;
$move2 = “Dangerous gust of wind (30 energy)”;
$move3 = “Small fireball, 10% chance of burn (25 energy)”;
}
elseif($class == “assassin”) {
$strength = 15;
$specialstrength = 5;
$defense = 8;
$specialdefense = 5;
$accuracy = 20;
$move1 = “Tackle”;
$move2 = “Swing secret sword”;
$move3 = “Restore health (45 energy)”;
}
else {
$strength = 18;
$specialstrength = 10;
$defense = 5;
$specialdefense = 5;
$accuracy = 8;
$move1 = “Crazily swing arms”;
$move2 = “Bite”;
$move3 = “Burp (75% chance of poison) (30 energy)”;
}

$sql = “INSERT INTO rpg (Name, Level, XP, Health, Email, Gold, Energy, Class, Strength, SpecialStrength, Defense, SpecialDefense, Accuracy, Move1, Move2, Move3) VALUES ($name, 1, 0, 100, $email, 1000, 50, $class, $strength, $specialstrength, $defense, $specialdefense, $accuracy, $move1, $move2, $move3)”;

if(!$sql) {
die(mysql_error());
}

else {
echo “Hero successfully created. <a href=“heroes.php”>Click here to return to the hero page.”;
}

?>[/php]

Give me the output of that as I cannot see what the var $class contains
[php]
$class = $_POST[‘class’];
echo ‘’.$class.’’; // checking for whitespace if it has a space between ** then it has whitespace

// if it has white space we remove it
$class = trim($class); // remove the whitespace
echo ‘’.$class.’’; // checking for whitespace
[/php]

also not sure what this is for as it does nothing

[php]
if(isset($_SESSION[‘email’])) { }
[/php]

Unexpected = on line 20.

Line 20: echo = ‘’.$class.’’; // checking for whitespace if it has a space between ** then it has whitespace

I assume that will happen again for line 24.

echo = ‘’.$class.’’; // checking for whitespace

You missed the values from the form it was looking for Assassin instead of assassin
Sry was quick was thiking about the var and not what i was typing echo does not need =
[php]

Assassin [/php]

[php]

Assassin [/php]

Oh, ok. I thought I just needed name.

It works now. Thanks!

Sponsor our Newsletter | Privacy Policy | Terms of Service