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!
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 :
[...]
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?
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...
<?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.
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.
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.
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.
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...
<?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)
<?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.
?>
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 )