Page 1 of 1

Undefined index error

Posted: Thu Nov 19, 2009 9:09 pm
by akeisha84
Hi i am working on registering and login users.
My registration works, and however it sends the person registering a confirmation email with a confirmation link that takes them to a confirmation page.
This page shows a success msg, indicating they have successfully registered and is now a memeber and needs to click on a link to login.
However at the point in time when the user clicks the confirmation link in the email this error msg comes up:

Notice: Undefined index: userid in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\eVoting\PHP\confirmation.php on line 30

The following is the confirmation code:

//connect to db


$id = $_GET['USERID'];
if ($_GET['USERID']==true) { the error is at this line 30
$query = mysql_query("SELECT * FROM USERS WHERE ACTKEY = '$id' LIMIT 1") or die(mysql_error());
$row = mysql_fetch_array($query);[/color]
if(mysql_num_rows($query) > 0) {

$userid = $row['USERID'];
$do = mysql_query("UPDATE USERS SET ACTIVATED = 1 WHERE USERID = '$userid' LIMIT 1") or die(mysql_error());

if($do) {
//messages if activation is successful or not
echo '
<p>Activation successful! You can now login!</p>
<p><a href="../html/ind.html">Click here</a> to goto the login page.</p>
';

} else {

echo '
<p>We are sorry, there appears to be an error processing your activation. Please try again later.</p>
';

}

} else {

echo '
<p>Sorry, your activation code was incorrect. Please try again.</p>
';

}
}//END TRUE

i have tried resolving it by using isset and replacing whats in the () with $id==true.
The same error still comes up, please help.

Note: i am no expert developer, so some simple terms or examples to resolve will be appreciated.
thank you...

Posted: Thu Nov 19, 2009 9:41 pm
by Jonah Bron
As you probably know, $_GET gets GET variables in the URL. This is an example of a URL with a GET variable:

http://example.com/some/page.php?someva ... meotherval

The question mark shows that everything after that is variables.
Somevar is the name of a variable, who's value is someval.
The ampersand shows where that variable ends, and where the next one begins, etc.

So, if you are trying to get $_GET['userid'], then you must make sure that the URL has userid=something in it.

Also, you should always use

Code: Select all

if (!isset($_GET['variable_you_are_trying_to_get'])){
    //Throw an error, or assign it as null
}
This avoids displaying an error (allowing you to catch it yourself), even if that value is not present.

Re: Undefined index error

Posted: Fri Nov 20, 2009 2:30 am
by Weiry
Jonah Bron wrote: Also, you should always use

Code: Select all

if (!isset($_GET['variable_you_are_trying_to_get'])){
    //Throw an error, or assign it as null
}
isset() can be useful, but you can run into a problem where the variable might be 0, "", etc which are set values, but are also empty.
A place where i would use isset(), is if im looking to see if a button has been pressed.

To allow the code to check to see if there is actually any data, just check if the variable is empty.

Code: Select all

if (!empty($_GET['USERID'])){