Jump to content
MakeWebGames

Error Reporting: Need Help With A Notice


Miks

Recommended Posts

Happy Easter everyone, well those that know the meaning, otherwise happy chocolate egg holiday!

Anyway, moving on to business.....

As my knowledge "slowly" grows I learn and come across new things, I have a play, hit a brick wall and then seek help!

So at the moment I am sticking error_reporting into all my work, I bet you can imagine the fun I am having now, the saying ignorance is bliss comes to mind!

I have only turned error reporting on to one mod, the city park. I am getting this notice, even though its a notice I would like to resolve it

Notice: Undefined index: spend in /home/public_html/citypark.php on line 27

 

<?php

include "globals.php";
error_reporting(E_ALL); 
if($ir['jail']) 
{ 
print "<div class='desc2'>
<div class='citstat'>
<table class='mytable'><tr><td><center><BR>
You cant go any further while you're in jail <BR><a href='jail.php'>Go back to jail</a>
<BR><BR></center></td></tr></table>";
$h->endpage(); 
exit; 
}

if($ir['hospital']) 
{ 
print "<div class='desc2'>
<div class='citstat'>
<table class='mytable'><tr><td><center><BR>
You can go any further while your in hospital <BR><a href='hospital.php'>Go back to Hospital</a>
<BR><BR></center></td></tr></table>";
$h->endpage(); 
exit; 
}

if(!$_GET['spend'])
{

print "
<div class='desc2'>
<div class='citstat'>
<style type='text/css'>
}
</style>
<h2>Welcome to the City Park!</h2>
<br />
You walk up to a weird looking fountain, you see an old glass with lots of scratches and brown patches.<BR><BR>
Do you take a drink?<BR>
<br />
<table class=mytable>
<th>Fountain</th>
<tr><td><a href='citypark.php?spend=yes'><center>Yes</center></a></td></tr>
<tr><td><a href='citypark.php?spend=no'><center>No!</center></a></td></tr>
</table> </div>
";
}
else
{
if($_GET['spend'] == 'yes')
{
if($ir['citypark'] == 1)
{
print "<div class='desc2'>
<div class='citstat'>This has no effect on you";
}
else
{
$db->query("UPDATE `users` SET `energy`=maxenergy,`citypark`=1 WHERE `userid`=$userid");
print "<div class='desc2'>
<div class='citstat'>You have an almighty gulp and magically it restores your energy, awesome!";
}
}

if($_GET['spend'] == 'no')
{
print "
<div class='desc2'>
<div class='citstat'>
Wise choice buddy wise choice!";
}
}  

$h->endpage();
?>

 

I'm hoping someone could explain what this means exactly and how to go about resolving it....maybe an example using my code to make it easier to understand?

I'm guessing I havent defined if(!$_GET['spend']) but how would I do that? Also why does this particular GET have a "!" at the beginning? If I remove "!" the feature stops working and displays all 3 warnings about undefined GET thingys (I have 3 undefined GETs in totol)

Edited by Miks
Link to comment
Share on other sites

Well, you were right that it has to do with this line:

 

if(!$_GET['spend'])

 

In relation to this error: "Notice: Undefined index: spend in /home/public_html/citypark.php on line 27"

This is because you are attempting to check the value of $_GET['spend'] without evaluating whether the variable is even set or not. (I have seen this MANY times and it is very poor programming practice to keep this as-is.)

So, to fix this problem, you will want to check whether or not $_GET['spend'] is set:

 

if(isset($_GET['spend']) && !$_GET['spend'])
{

print "
<div class='desc2'>
<div class='citstat'>
<style type='text/css'>
}

 

isset() in PHP manual: http://php.net/manual/en/function.isset.php

That should remove the notice. This notice is actually pretty informative as to what the problem is, so if you encounter a notice like this again, watch for these evaluation statements (if, switch, while, for) checking variables. Make sure that variable is explicitly set first before evaluating its value. If not, then you should first evaluate whether it is set or not BEFORE evaluating its value. :)

Hope this helps!

~G7470

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...