Page 1 of 1

Undefined Index error

Posted: Sat Aug 13, 2005 11:00 pm
by blindchild
Hi,
I am relatively new to php and decided to learn by doing and so I made myself a sort of blog.
95% of the site works fine, but one problem that I am having is an undefined index error that is generated on one of my pages. Its quite annoying and messes up the whole layout of the page. The error is as follows:

Notice: Undefined index: KT_id_blindblog in /usr/local/psa/home/vhosts/blindchild.com/httpdocs/blog/byArticle.php on line 117

Now I think I know why the error is appearing, its because no one has actually logged into the page and so there is no value for the KT_id_blindblog session variable. Ive rattled my brains to try and work out how to fix the problem, but I am getting no where. Getting rid of the error one way will simply generate an error somewhere else.

The page can be viewed here: http://www.blindchild.com/blog/byArticle.php?id_art=4.

Any advice, suggestions or links would really help me out. I'm at a dead end here.

Thank you
Ross[/url]

Posted: Sat Aug 13, 2005 11:03 pm
by feyd
hint:

Code: Select all

isset($_SESSION['KT_id_blindblog'])

So close...yet so far

Posted: Sat Aug 13, 2005 11:26 pm
by blindchild
Thanks for the hint, I did try that before but it generated another error when it actually came to a user logging in and posting.
Ill give it another go, thanks for the hint, it seems I'm close.
Thanks
Ross
PS. Ill let you know if I sort it. Thanks again.

Posted: Sat Aug 13, 2005 11:34 pm
by s.dot
put it in an if/else

Code: Select all

if(isset($_SESSION['KT_id_blindblog']))
{
     // the session variable is set, so do this
} ELSE
{
    // the session variable is not set, so do this
}

Posted: Sat Aug 13, 2005 11:40 pm
by John Cartwright
... more along the lines of

Code: Select all

$_SESSION['KT_id_blindblog'] = (isset($_SESSION['KT_id_blindblog']) ? $_SESSION['KT_id_blindblog'] : 'default_value_goes_here');

//$_SESSION['KT_id_blindblog'] will always have been initialized by this point
echo $_SESSION['KT_id_blindblog'];

Maybe....

Posted: Sat Aug 13, 2005 11:42 pm
by blindchild
Hi again,
I just gave it another go and it appears to have worked. But just to check, this is what I did.
I inserted the line:

Code: Select all

if (isset($_SESSION['KT_id_blindblog'])){
before the default values are defined.
If I'm wrong let me know.
Thanks
Ross

**EDIT** Wow just saw some more poeple had posted. Ignore my attempt till Ive read yours, incase I have embarrassed myself. Thanks

Both solutions work

Posted: Sat Aug 13, 2005 11:54 pm
by blindchild
Hi,
Thanks for all your help, the page is functioning properly now.
Any ideas about which suggestion is the best? Because both seem to work fine...at the moment I am rolling with Jcart's suggestion, purely because he has more posts.
Thanks for the quick responses.
Ross

Posted: Sun Aug 14, 2005 12:00 am
by feyd
that depends on how you want your code to function, but Jcart's will guarantee the variable existing.

Neither will guarantee the value being valid 100% of the time though, so you should still validate it.