Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Notice: Undefined index problems ..

Status
Not open for further replies.

hlpang2002

Programmer
Feb 22, 2006
5
GB
<?php

if (isset($_POST['username'])) {

// check username
$query = "SELECT username FROM members " .
"WHERE username = '" . $_POST['username'] . "';";
$result = mysql_query($query)
or die(mysql_error());

if (mysql_num_rows($result) != 0) {
?>
<span class="bold"><?php echo $_POST['username']; ?></span>
<span class="warning">, is already in use, please choose another!</span><br /><br />

<?php
}


// check email
$query = "SELECT email FROM members " .
"WHERE email = '" . $_POST['email'] . "';";
$result = mysql_query($query)
or die(mysql_error());

if (mysql_num_rows($result) != 0) {
?>
<span class="bold"><?php echo $_POST['email']; ?></span>
<span class="warning">, is already in use, please choose another!</span><br /><br />

<?php
}
} else {

$query = 'INSERT INTO members (username, password, surname, forename, email)
VALUES(\''.$_POST['username'].'\',
PASSWORD(\''.$_POST['password'].'\'),
\''.$_POST['surname'].'\',
\''.$_POST['forename'].'\',
\''.$_POST['email'].'\');
';






$result = mysql_query($query)

or die(mysql_error());


}
?>


<form name="frmReg" method="post" action="">
Username <input type="textfield" name="username" /><br />
Password <input type="password" name="password" /><br />
Retype Password <input type="password" name="password2" /><br />
Surname <input type="textfield" name="surname" /><br />
Forename <input type="textfield" name="forename" /><br />
Email <input type="textfield" name="email" /><br /><br />
<input type="button" value="submit" onClick="javascript:validate()" />
</form>
 
I need help in this code .. somebody please help me to see what happen on it .. thanks ...

the error is here .. :

PHP Notice: Undefined index: username in C:\Program Files\Apache Group\Apache2\htdocs\register5.php on line 175 PHP Notice: Undefined index: password in C:\Program Files\Apache Group\Apache2\htdocs\register5.php on line 176 PHP Notice: Undefined index: surname in C:\Program Files\Apache Group\Apache2\htdocs\register5.php on line 177 PHP Notice: Undefined index: forename in C:\Program Files\Apache Group\Apache2\htdocs\register5.php on line 178 PHP Notice: Undefined index: email in C:\Program Files\Apache Group\Apache2\htdocs\register5.php on line 179
 
Reformatting your code:

Code:
<?php

if (isset($_POST['username']))
{
    //    check username
    $query = "SELECT username FROM members WHERE username = '" . $_POST['username'] . "';";
    $result = mysql_query($query) or die(mysql_error());

    if (mysql_num_rows($result) != 0)
    {
    	print '<span class="bold">' . $_POST['username'] . '</span>
        <span class="warning">, is already in use, please choose another!</span><br /><br />';
    }

    //    check email
    $query = "SELECT email FROM members WHERE email = '" . $_POST['email'] . "';";
    $result = mysql_query($query)
        or die(mysql_error());

    if (mysql_num_rows($result) != 0)
    {
    	print '
        <span class="bold">'. $_POST['email'] . '</span>
        <span class="warning">, is already in use, please choose another!</span><br /><br />';
	}
}
else
{

      $query = 'INSERT INTO members (username, password, surname, forename, email)
      VALUES(\''.$_POST['username'].'\',
              PASSWORD(\''.$_POST['password'].'\'),
              \''.$_POST['surname'].'\',
              \''.$_POST['forename'].'\',
              \''.$_POST['email'].'\');
              ';

      $result = mysql_query($query) or die(mysql_error());
}
?>

That output is to be expected.

You have a line which checks for the existence of a form field's value:

if (isset($_POST['username'])) {

but it is in the else-clause of that same conditional that you reference the variables. Your test will have already proven that they do not exist.



Want the best answers? Ask the best questions! TANSTAAFL!
 
you want to look into this to also cleanup you code

Code:
<?

if (isset($_POST['username'])) {

	$username=$_POST['username'];
	$password=$_POST['password'];
	$surname=$_POST['surname'];
	$forename=$_POST['forename'];
	$email=$_POST['email'];
	


	//    check username
	$query = "SELECT username FROM members WHERE username = '$username' ";
	$result = mysql_query($query) or die(mysql_error());

	if (mysql_num_rows($result) != 0) {

	echo "<span class=\"bold\"><?=$username?></span>
        <span class=\"warning\">, is already in use, please choose another!</span><br /><br />";


	}


	//    check email
	$query = "SELECT email FROM members WHERE email = '$email'";
	$result = mysql_query($query) or die(mysql_error());

	if (mysql_num_rows($result) != 0) {

	echo "<span class=\"bold\"><?=$email?></span>
        <span class=\"warning\">, is already in use, please choose another!</span><br /><br />";

	}

} else {

      $query = "INSERT INTO members (username, password, surname, forename, email)
		VALUES('$username',PASSWORD('$password'),'$surname','$forename','$email' ");

      $result = mysql_query($query)

      or die(mysql_error());


}
?>
 
As a general rule, I recommend strongly against things like:

$username=$_POST['username'];
$password=$_POST['password'];
$surname=$_POST['surname'];
$forename=$_POST['forename'];
$email=$_POST['email'];

In a complex script the above code can be separated by quite a number of lines. If you don't look at the code for several months, you may not remember how $username got its value. If you reference $_POST['username'] every time, it's self-documenting.



Want the best answers? Ask the best questions! TANSTAAFL!
 
i understand where your coming from but i'm use too writing objects which is similar to declaring variables.

In a couple of months if your not sure what the variable $username is - take a closer look at your code. You should be doing that anyway to remind yourself where your at.

I believe that time is money and if you had $_POST['username'] 50 times on the page, it would take a while if you had to rename the field, not to mention escaping single, etc.

But thats my opinion.
 
well .. first of all .. thanks to many to reply my question .. i still not understand .. is that i have do wrong in this

if (isset($_POST['username'])) {

??

well, i have javascript validation also .. after click the button, it will validate all the things that users key in .. if there is no error in the validation, then it will check for the username and email whether they are duplicate or not ..

after the all check, only do the INSERT section .. so ~ any idea how to edit the code to make it without any PHP Notice: Undefined index problems ...

thanks ...
 
i think what sleipnir214 was say was that your never even reaching the insert part

Code:
if (isset($_POST['username'])) {
 ... select using $_POST['username']
}else{
... insert $_POST['username']
... but $_POST['username'] is not set so its not entering anything
}
 
The basic stucture of your script is:

if (isset($_POST['username']))
{
//do some "stuff"
}
else
{
//do some "other stuff"
}

If $_POST['username'] does not exist, it is because no form was submitted. However, the "other stuff" actions include references to the missing values from the form.



Want the best answers? Ask the best questions! TANSTAAFL!
 
well .. thanks to everyone here .. finally, i solved this already .. =) ...

thanks for your all help .. appreciate so much ..

cheers ! .. =)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top