Quick easy error, php xperts help!

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

I3lade
Forum Commoner
Posts: 70
Joined: Wed Jun 04, 2003 4:20 pm

Post by I3lade »

Ok, there arnt any error messages anymore.
I want it to say if u have a pet :
Error, you cannot make another pet!
And if you dont the stuff to create it.
So i did that code above which says if havepet is set to one it shows Sorry you cannont create another pet! and the else to show if its not 1 the stuff to make the pet. Right now all it shows is the stuff to make the pet even if its set to 1, whats wrong?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

where

Post by phpScott »

Where is havepet comming from. Is it a session variable or is comming from the db somewhere?

phpScott
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

As a test check the variable has a value in before using it in your if statement and then see if its equal to 1:

Code: Select all

...
session_start();
if(isset($havepet))
{
   if($havepet==1) { 
      echo "You already have a pet, you cannot create another!";
   }else{ 
      // do your other stuff.
   }
} else {
   echo "havepet not set!";
   var_dump($havepet);
}
...
Regards,
I3lade
Forum Commoner
Posts: 70
Joined: Wed Jun 04, 2003 4:20 pm

Post by I3lade »

Well havepet is a int feild in one of my database tables
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

Can you guarantee that $havepet has a value, and that value is 1 ?

Echo out the variable or put more matching/testing in your script to handle the possible values of this variable.

Regards,
I3lade
Forum Commoner
Posts: 70
Joined: Wed Jun 04, 2003 4:20 pm

Post by I3lade »

?Are you saying the feild in the db isnt right?
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

No, just that the variable may not contain a value of 1, your if statement is matching something which obviously is'nt a value of 1.

As a test, echo out $havepet before the if (after you start your session) and see what it's value is:

Code: Select all

echo "havepet value = ".$havepet."<br />\n";
What is the value of this variable ?

Regards,
I3lade
Forum Commoner
Posts: 70
Joined: Wed Jun 04, 2003 4:20 pm

Post by I3lade »

Well, sorry to post this so late was on vacation, each persons value is different. The value is either 1 or 0 but when they fill out a form on a sperate page it sets the value to one. Mine was set to one but still thinks it's 0. Maby I need to get thier id or something otherwise it checks no row wihich would mean it would be 0, which is defualt.
Post Reply