5 Replies - 1253 Views - Last Post: 06 October 2009 - 12:03 AM Rate Topic: -----

#1 Hirthas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-November 08

Undefined index but i have no idea why

Post icon  Posted 05 October 2009 - 10:39 AM

Hi i could really use some help here. I am making a account management site in php and I am getting this error that I'm not sure how to fix or why i am getting it. Here is the error that comes up when I try to register a new account:

Notice: Undefined index: userID in c:\Inetpub\wwwroot\356\vmicchia\Project1\createNewAccount.php on line 5

Notice: Undefined index: passwd in c:\Inetpub\wwwroot\356\vmicchia\Project1\createNewAccount.php on line 7

Notice: Undefined index: passwd2 in c:\Inetpub\wwwroot\356\vmicchia\Project1\createNewAccount.php on line 8

Notice: Undefined index: userID in c:\Inetpub\wwwroot\356\vmicchia\Project1\createNewAccount.php on line 9

Here is the form that is being submitted:
 <form method="post" id="form0" action="createNewAccount.php">
		<ul>
		  <li>
			<label for="login">User Name:</label>
			<input id="userID" name="userID" type="text" size="20" maxlength="20" />
		  </li>
		  <li>
			<label for="pass">Password:</label>
			<input id="passwd" name="passwd" type="password" size="20" maxlength="20" />
		  </li>
		  <li>
			<label for="pass2">Confirm Password:</label>
			<input id="passwd2" name="passwd2" type="password" size="20" maxlength="20" />
		  </li>
<li>
			<input id="submit" name="submit" type="submit" value="Register" />
		  </li>
		  <li><?php echo $_SESSION["errorMsg"]; ?></li>
		</ul>


and here is the pass through page it goes to:

<?php
//start the session
	session_start();
	
	$login = $_POST["userID"];
	//echo $login;
	$pass1 = md5($_POST["passwd"]);
	$passConfirm = md5($_POST["passwd2"]);
	$_SESSION["login"] = $_POST["userID"];
	$passMatch = false;
	
	if ((empty($_POST["login"])) || (empty($_POST["pass"])) || (empty($_POST["pass2"]))) /*||  (empty($_POST["lastName"])) || (empty($_POST["firstName"])) || (empty($_POST["email"])) || (empty($_POST["news"])))*/{
		$_SESSION["errorMsg"] = "Please make sure to fill out all the fields.";
		header("Location:index.php");
		exit;
	}
	
	include("includes/openDbConn.php");
	
	if ($pass1 == $passConfirm){
		$sql="SELECT Login FROM employee WHERE Login='".$login."'";
		//echo $sql;
		
		$result=mysql_query($sql);
		
		//nested if to check if the username is taken (if result)
		if(empty($result)){
			$num_results = 0;
		}else{
			$num_results = mysql_num_rows($result);
		}//end if result
		
		if ($num_resukts = 0){
			$sql="INSERT INTO employees(Login, Passwd) VALUES ('".$login."', '".$pass1."')";
			//echo $sql;
			
			$result=mysql_query($sql);
			
			$_SESSION["errorMsg"] ="You have been added successfully.";
			
			$passMatch = true;
			
		}else{
			$_SESSION["errorMessage"] = "User Name already exists";
		$passMatch = false;
		}//end if taken
	}else{
		$_SESSION["errorMessage"] = "Your Passwords do not match.";
		$passMatch =false;
	}
	
	if ($passMatch){
		CleanUp();
		header("Location:userHome.php");
		exit;
	}else{
		CleanUp();
		header("Location:createNewAccount.php");
		exit;
}

	
	
//this function clears out all variables used on this page.																																																																									
function CleanUp(){
	$login="";
	$pass1="";
	$passConfirm="";
	$passMatch="";
	$sql="";
	$result="";
	$num_results="";
}

?>


any help would be really great thanks. I will be adding more elements later but this is just to test and make sure it works.

This post has been edited by Hirthas: 05 October 2009 - 10:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Undefined index but i have no idea why

#2 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Undefined index but i have no idea why

Posted 05 October 2009 - 10:51 AM

That error signifies you're trying to access a key that doesn't exist on some array.

So you need to change things like:
$login = $_POST["userID"];

to
$login = (isset($_POST["userID"]) ? $_POST["userID"] : null); //Basically, "if $_POST["userID"] is set, then $login is set to it, otherwise it's null."


Or turn off the display of errors, the choice is yours.

Yours,
Shane~

This post has been edited by ShaneK: 05 October 2009 - 10:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Undefined index but i have no idea why

Posted 05 October 2009 - 10:59 AM

I dont see a closing form tag in the HTML you posted (Is it in the HTML and you forgot to post it?). If it's not in the HTML page then add it.

When setting your variables try using isset() before setting a value (as the post array may not be populated when the page first executes). Something like this

if(isset($_POST["userID"]))
{
	   $login = $_POST["userID"];
}


Was This Post Helpful? 0
  • +
  • -

#4 Hirthas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-November 08

Re: Undefined index but i have no idea why

Posted 05 October 2009 - 12:02 PM

OK I used the isset idea and it gets rid of the errors. However my big problem is it still isn't reading anything into the variables so I can't insert anything into the DB. Also, there is an </form> tag just didn't copy paste enough.
Was This Post Helpful? 0
  • +
  • -

#5 cmwise  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 169
  • Joined: 14-February 09

Re: Undefined index but i have no idea why

Posted 05 October 2009 - 07:26 PM

Your Code:
if ($num_resukts = 0){


mispelled results! :P
Was This Post Helpful? 0
  • +
  • -

#6 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Undefined index but i have no idea why

Posted 06 October 2009 - 12:03 AM

You'll also want it to be if ($num_results === 0){, as the way you presently have it will set $num_resukts to 0 and evaluate to true. The line above fixes the typo and checks the value as opposed to assigning it. The fact that I used a triple equals there instead of a double shows that it's also checking the type, so if $num_results for some reason comes up false (and doesn't halt the script because of it), it doesn't execute the insert query.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1