Gossamer Forum
Home : General : Internet Technologies :

Undefined Variable: Error. Why??

Quote Reply
Undefined Variable: Error. Why??
Ive got a script here that gives me the error 'Undefined Variable: id' in it. I had the $PHP_SELF give errors before i replaced it with '$_SERVER['PHP_SELF']' if that helps any. Im running it off an IIS server.

Can anyone tell me how to get rid of this error and make my script work?

Code:
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
if ($id) {
$result = mysql_query("SELECT * FROM employees WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
printf("First name: %s\n<br>", $myrow["first"]);
printf("Last name: %s\n<br>", $myrow["last"]);
printf("Address: %s\n<br>", $myrow["address"]);
printf("Position: %s\n<br>", $myrow["position"]);
} else {
// show employee list
$result = mysql_query("SELECT * FROM employees",$db);
if ($myrow = mysql_fetch_array($result)) {
// display list if there are records to display
do {
printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $_SERVER['PHP_SELF'], $myrow["id"], $myrow["first"], $myrow["last"]);
} while ($myrow = mysql_fetch_array($result));
} else {
// no records to display
echo "Sorry, no records were found!";
}
} ?> </body>
</html>
Quote Reply
Re: [TheIceman] Undefined Variable: Error. Why?? In reply to
I moved the thread to the "Internet Technologies" forum, because your question is a PHP question and not a Links SQL PHP frontend question.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [TheIceman] Undefined Variable: Error. Why?? In reply to
Can you post the exact error message, i.e with line references etc...

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Undefined Variable: Error. Why?? In reply to
In Reply To:
Can you post the exact error message, i.e with line references etc...

Cheers
Notice: Undefined variable: id in c:\inetpub\wwwroot\test\index.php on line 6
Quote Reply
Re: [TheIceman] Undefined Variable: Error. Why?? In reply to
Well, I'm not sure, but it looks like your php configuration may have register_globals set to off. That would explain why $PHP_SELF needed to be replaced by $_SERVER["PHP_SELF"]. If that's the case, then you need to specify where the script should be looking for the $id variable. In other words, it should probably be one of the following:

$_POST["id"]
$_GET["id"]
$_COOKIE["id"]

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] Undefined Variable: Error. Why?? In reply to
Nope, that didnt fix it, when i tried $_POST["id"] i got the error:

Notice: Undefined index: id in c:\inetpub\wwwroot\test\index.php on line 7


And when i tried $_GET["id"] i got:

Notice: Undefined variable: id in c:\inetpub\wwwroot\test\index.php on line 8

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\inetpub\wwwroot\test\index.php on line 9




Can anyone else help me out here?
Quote Reply
Re: [TheIceman] Undefined Variable: Error. Why?? In reply to
How is the $id variable being passed to the script? Get or post? If it's in the url, then it's get. If it's through a form with method="post", then it's post.

Assuming it's get, try:

Code:


$id = $_GET["id"];
if ($id) {
$result = mysql_query("SELECT * FROM employees WHERE id='$id'");
If it's a post variable, then just change $_GET["id"] to $_POST["id"]. Let me know if that brings up any new errors.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] Undefined Variable: Error. Why?? In reply to
Nope, same errors, undefined variable. Is it to do with globals or something? how do i turn them on and off on an IIS system running on winXP?
Quote Reply
Re: [TheIceman] Undefined Variable: Error. Why?? In reply to
How about adding something like this at the top of your scripts;

Code:
foreach($HTTP_GET_VARS as $thing => $var) { $$thing = $var; }

foreach($HTTP_POST_VARS as $thing => $var) { $$thing = $var; }

foreach($_GET as $thing => $var) { $$thing = $var; }

foreach($_POST as $thing => $var) { $$thing = $var; }

Do this with any other array of variables that you need to access as globals. Its pretty simple code that I had to write a while back, to get over a similar problem you are having now.

Hope it helps Smile

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Undefined Variable: Error. Why?? In reply to
Just like to warn you that doing that isn't the smartest thing to do (and the reason they've finally turned the register_globals option off by default). Anyways, if you insist on doing it, you can turn register_globals on (if you have permissions to do so), or you can use:

http://www.php.net/extract

Which essentially does the same as what Andy pasted, but it's must prettier and you have more control over duplicate variable names and such.

Adrian
Quote Reply
Re: [hennagaijin] Undefined Variable: Error. Why?? In reply to
Also note that they only added the _GET, etc globals in 4.2.x (can't remember which one exactly). You'll also want to check the value of id (or at least intval() it). Lastly, are you passing in this value to the script itself (eg. foo.com/foo.php?id=123)?

Adrian
Quote Reply
Re: [brewt] Undefined Variable: Error. Why?? In reply to
What are you doing at this time of morning Adrian? Laugh

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Undefined Variable: Error. Why?? In reply to
"studying" for a final exam I have to write in 5 hours Smile

Adrian
Quote Reply
Re: [brewt] Undefined Variable: Error. Why?? In reply to
yeah, i have links on the page like

Code:
<a href="index.php?id=123">Click me to see 123</a>
Quote Reply
Re: [brewt] Undefined Variable: Error. Why?? In reply to
Wow...an exam? I thought you were at least 20? Unsure (sorry if thats an insult)...I'm almost 19, and I dont have anywhere near the knowledge of programming as you Frown what did I do wrong? lol

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Undefined Variable: Error. Why?? In reply to
>>
Wow...an exam? I thought you were at least 20?
<<

Erm, what makes you think people over 20 don't have exams?......I knew mature students at university who were in their 40's

Not really on topic for the thread ;)
Quote Reply
Re: [Paul] Undefined Variable: Error. Why?? In reply to
OI, HOW about keeping this on topic, this board has PM's you know, use them if you want to private chat, or start a new topic somehwere, i got a legit question that im totally stuck on here and i dont want it closed cause it wanders off topic.

Now can anyone give me any more clues here?

If register_globals are going to be off by default in the new versions of PHP, will this stuff up everyones script? they surely cant leave it off, they would have to turn it on or they will stuff hundreds of thousands of scripts wont they?? If i turn register_globals on, will this fix my problem? how do i turn on register_globals on an XP machine running IIS5??

later...
Quote Reply
Re: [TheIceman] Undefined Variable: Error. Why?? In reply to
>>>OI, HOW about keeping this on topic, <<<

Wow...you certainly know how to talk to people, so they are not interested in helping you Wink

Although I don't really wanna answer your question, I'm gonna. All you need to do is turn register_globals on. Don't worry about how it will work with other servers. Its completly up to the other server admin as to if they want register_globals turned on. There is ways around it, as Adrian explained Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!