Undefined index error

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!

Moderator: General Moderators

Post Reply
akeisha84
Forum Newbie
Posts: 1
Joined: Thu Nov 19, 2009 8:56 pm

Undefined index error

Post 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...
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post 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.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Undefined index error

Post 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'])){
Post Reply