Page 1 of 1

an undefined variable problem

Posted: Tue Dec 26, 2006 2:18 am
by tarja311
Hey guys,

I have a script that displays a list of user-ids, each having their own hyper-link that when clicked, executes an sql command to insert that user-id from one table to another. So far everything works, but only when i replace $id with the actual USERID in the database :

Code: Select all

mysql_query("INSERT INTO `TABLE2` (`MEMBERID`) SELECT `USER` FROM `TABLE1`  WHERE `USERID` = '14'");


Everytime i try with $id i receive : "Notice: Undefined variable: id in C:\test.php on line 48"

Here is my code. Hopefully somebody can point me in the right direction.

Code: Select all

if(!isset($cmd))
{ 
	$result = mysql_query("SELECT * FROM `TABLE1`");	
	
	echo "USER ID :<BR><HR><BR>";
	while($r = mysql_fetch_array($result))
	{
	 	$id	= $r["USERID"];
	 	$user	= $r["USER"];
	 	
	 	echo "<LI>". $user ." <A HREF = '?show=test&cmd=member&id=$id'>Click Me</A>";
	 	echo "<BR><BR>";	 	
	} // end while-loop.
	
} 
else
{
	if($cmd == "member")		// set member.
	{	
	 	// grab data from TABLE1.USERID, insert it into TABLE2.MEMBERID.
		mysql_query("INSERT INTO `TABLE2` (`MEMBERID`) SELECT `USER` FROM `TABLE1`  WHERE `USERID` = '$id'");
		echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=?show=test'>";
	} // end if.
} // end else isset.
I guess i lose the variable's scope after the if statement??

Thanks

-- tarja

Posted: Tue Dec 26, 2006 4:14 am
by spamyboy
witch is 48 line ?

Re: an undefined variable problem

Posted: Tue Dec 26, 2006 4:43 am
by volka
tarja311 wrote:

Code: Select all

[...]
else
{
	if($cmd == "member")		// set member.
	{	
	 	// grab data from TABLE1.USERID, insert it into TABLE2.MEMBERID.
		mysql_query("INSERT INTO `TABLE2` (`MEMBERID`) SELECT `USER` FROM `TABLE1`  WHERE `USERID` = '$id'");
		echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=?show=test'>";
	} // end if.
} // end else isset.
Where does $id come from in this block? It's used in the sql statement, but where is a value assigned to it?

Posted: Tue Dec 26, 2006 11:12 am
by RobertGonzalez
Your error message, "Notice: Undefined variable: <variable_name> in <file_name> on line <line_number>", is common to the situation in which you attempt to access a variable's value on a variable that has not yet been initialized. Something like this...

Code: Select all

<?php
// $this_var has not yet been set to anything
// So this will throw your error
if ($this_var)
{
  // do something
}
?>
Make sure to either initialize the variable or make sure you are in the proper conditional scope so that the declared variable actually is valuated within the scope of its use.

Posted: Tue Dec 26, 2006 12:12 pm
by tarja311
spamyboy wrote:witch is 48 line ?
Line 48 would be this line :

Code: Select all

mysql_query("INSERT INTO `TABLE2` (`MEMBERID`) SELECT `USER` FROM `TABLE1`  WHERE `USERID` = '$id'");
volka wrote:Where does $id come from in this block? It's used in the sql statement, but where is a value assigned to it?
It does not come from that block, it comes from the top code. How can i get the value from the top code down into the bottom code? I've seen other instances of the same script do this where it worked fine, even with the undefined variable notice. I just don't get it. :?
Everah wrote: Make sure to either initialize the variable or make sure you are in the proper conditional scope so that the declared variable actually is valuated within the scope of its use.
I understand but how to accomplish this? The variable is supposed to be initialized ( i think ? ) when the hyper-link is clicked on.

-- tarja

Posted: Tue Dec 26, 2006 12:21 pm
by volka

Code: Select all

if(!isset($cmd))
{
 // #1       
}
else
{
 // #2
}
If the code block #2 is executed, #1 isn't.
Only in #1 $id is defined and gets a value assigned, not in #2.

Posted: Tue Dec 26, 2006 12:29 pm
by onion2k
I imagine register_globals is switched off, so you'll need to do something like:

Code: Select all

$id = mysql_real_escape_string($_GET['id']);
Do that prior to using $id in your SQL and it should be ok.

By the way, are TABLE1 and TABLE2 your real database table names? You might want to name them something a bit more descriptive.

Posted: Tue Dec 26, 2006 12:49 pm
by RobertGonzalez
tarja311 wrote:
Everah wrote: Make sure to either initialize the variable or make sure you are in the proper conditional scope so that the declared variable actually is valuated within the scope of its use.
I understand but how to accomplish this? The variable is supposed to be initialized ( i think ? ) when the hyper-link is clicked on.
It shouldn't be, unless register_globals is on, which is very bad. You should be assigning your variables with values prior to using them or running them through isset() to see if they have been initialized.

Posted: Tue Dec 26, 2006 1:53 pm
by tarja311
onion2k wrote:I imagine register_globals is switched off, so you'll need to do something like:

Code: Select all

$id = mysql_real_escape_string($_GET['id']);
Do that prior to using $id in your SQL and it should be ok.

By the way, are TABLE1 and TABLE2 your real database table names? You might want to name them something a bit more descriptive.
Actually, the funny thing is register_globals is set to 'On', by default. I tried the code you provided above the SQL like so :

Code: Select all

if($cmd == "member")        // set member.
{       
 	$id = mysql_real_escape_string($_GET['id']);
 	
    // grab data from TABLE1.USERID, insert it into TABLE2.MEMBERID.
    mysql_query("INSERT INTO `TABLE2` (`MEMBERID`) SELECT `USER` FROM `TABLE1`  WHERE `USERID` = '$id'");
    echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=?show=test'>";
} // end if.
but i receive a : Notice: Undefined index: id in...

Unless i am going about this all wrong... ( i am not an expert by any means. ) :?

btw -- the tables are not TABLE1 and TABLE2 in the actual database. I named them that here so it can be easy to distinguish the two.
Everah wrote: You should be assigning your variables with values prior to using them or running them through isset() to see if they have been initialized.
But what would it be pre-initialized to? It's supposed to use the id found in the hyper-link.

I apologize for sounding so newbie-ish. It's taken me hours to figure out this "tiny" problem on my own.

Posted: Tue Dec 26, 2006 2:25 pm
by RobertGonzalez
After looking at your code, your problem is that you are stuck inside a conditional scope. Here is your code with some comments, followed by some code that should work for you...

Your code:

Code: Select all

<?php
// Check if $cmd is not set (initialized)
if (!isset($cmd))
{
    // Run a query - ITS OK I DON'T NEED ANY ERROR CHECKING ON MY QUERIES - tsk tsk
    $result = mysql_query("SELECT * FROM `TABLE1`");
       
    echo "USER ID :<BR><HR><BR>";
    
    // Quick note, if the query failed this will puke horribly on you
    while ($r = mysql_fetch_array($result))
    {
        // Let's create the vars $id and $user
        $id    = $r["USERID"];
        $user  = $r["USER"];

        // and now we are going to echo out a link with the $id var we just made inside this if/while construct
        echo "<LI>". $user ." <A HREF = '?show=test&cmd=member&id=$id'>Click Me</A>";
        echo "<BR><BR>";             
    } // end while-loop.
}
else
{
    // Ok, if we are inside this block, then $cmd was set to something... but what?
    if($cmd == "member")
    {       
        // $cmd was set to member
        // grab data from TABLE1.USERID, insert it into TABLE2.MEMBERID.
        
        /* 
            The challenge here is that you need $id, but it is not set anywhere 
            except in the scope of the other half of this conditional
        */
        // AGAIN, I NEED DON'T NEED TO SHOW YOU NO STINKING ERROR CHECKING
        mysql_query("INSERT INTO `TABLE2` (`MEMBERID`) SELECT `USER` FROM `TABLE1`  WHERE `USERID` = '$id'");
        
        echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=?show=test'>";
    } // end if.
} // end else isset. 
?>
My code: (this is usable, but not guaranteed. Play around with this to get it to do what you want it to)

Code: Select all

<?php
$show = '';
$cmd = false;
$id = 0;

if (isset($_GET['show']))
{
    $show = $_GET['show'];
}

if (isset($_GET['cmd']))
{
    $cmd = $_GET['cmd'];
}

if (isset($_GET['id']))
{
    $id = intval($_GET['id']);
}

// Check if $cmd is not set (initialized)
if (!$cmd)
{
    // Run a query - WITH A TEENSEY BIT OF ERROR CHECKING
    $result = mysql_query("SELECT * FROM `TABLE1`") or die(mysql_error());
       
    echo "USER ID :<BR><HR><BR>";
    
    while ($r = mysql_fetch_array($result))
    {
        // Let's create the vars $id and $user
        $id    = $r["USERID"];
        $user  = $r["USER"];

        // and now we are going to echo out a link with the $id var we just made inside this if/while construct
        echo "<LI>". $user ." <A HREF = '?show=test&cmd=member&id=$id'>Click Me</A>";
        echo "<BR><BR>";             
    } // end while-loop.
}
else
{
    // Ok, if we are inside this block, then $cmd was set to something... but what?
    if($cmd == "member")
    {       
        // Quick test
        echo '<h1>The selected user id is ' . $id . '</h1>';
        
        mysql_query("INSERT INTO `TABLE2` (`MEMBERID`) SELECT `USER` FROM `TABLE1`  WHERE `USERID` = $id") or die(mysql_error();
        
        echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=?show=test'>";
    } // end if.
} // end else isset. 
?>

Posted: Tue Dec 26, 2006 3:11 pm
by tarja311
Thank you very much. I was able to work with the example code you provided. :)

I appreciate the help.

-- tarja

Posted: Tue Dec 26, 2006 3:17 pm
by RobertGonzalez
You're welcome. I would suggest, when you have time, to sift through the code to make sure you understand what is happening.

Posted: Tue Dec 26, 2006 3:30 pm
by tarja311
Gotcha. This has been a very big project of mine for some time now. It's very unfortunate when you get stuck on something so simple, that it takes away almost an entire day of your time, when you could have used that time to work on the more important stuff.

It is also very important to me to understand what it is doing underneath the eye-candy UI. Makes it easier to explain to the programmers after me what it is doing. ;)

Again thanks for your help. ( and for the sql error-checking )

--tarja :)