Jump to content

[SOLVED] Notice: Undefined Index


jreed2236

Recommended Posts

I'm making a register/login form and I keep getting this error:

 

indexerror.jpg

 

 

here is my code:

 

<?php
echo "<h1>Register</h1>";
//form data
$submit = $_POST['submit'];
$fullname = strip_tags($_POST['fullname']);
$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = ("date.America/New_York");
$email = $_POST['email'];

if ($submit)
{

   //open database
   $connect = mysql_connect("-","-","-");
   mysql_select_db("-"); //select database

   $namecheck = mysql_query("SELECT username FROM users WHERE username='$username'");
   $count = mysql_num_rows($namecheck);

   if ($count!=0)
   {
    die("Username Taken.");
   }

//check for existance
if ($fullname&&$username&&$password&&$repeatpassword)
  {

    if ($password==$repeatpassword)

    {
     //check char length of username and fullname
     if (strlen($username)>25||strlen($fullname)>25)
     {
      echo "Length of username is too long!";
     }
     else
     {

      //check password length
      if (strlen($password)>25||strlen(password)<6)
      {
       echo "Password must be between 6 and 25 characters.";
      }
      else
      {
       //register user!
       
       //encrypt password
    $password = md5($password);
    $repeatpassword = md5($repeatpassword);


       //generate random number for activation proccess
       
       $random = rand(23456789,98765432);
      
      $queryreg = mysql_query("
      
      
      INSERT INTO users VALUES ('','$fullname','$username','$password','$email','$date','$random','0')

      ");
      
      die("You have been registered! <a href='http://www.zomvie.com/index.php'>Return to login page</a>");


      }

     }

    }
    else
        echo "Your passwords do not match.";
}
else
     echo "Please fill in <b>all</b> fields";
}


?>

<html>
<p>
<form action='http://www.zomvie.com/register.php' method='POST'>
       <table>
              <tr>
                  <td>
                  Your full name:
                  </td>
                  <td>
                   <input type='text' name='fullname' value='<?php echo $fullname; ?>'>
                  </td>
               </tr>

                             <tr>
                  <td>
                  Choose a username:
                  </td>
                  <td>
                   <input type='text' name='username'value='<?php echo $username; ?>'>
                  </td>
               </tr>

                             <tr>
                  <td>
                  Choose a password:
                  </td>
                  <td>
                   <input type='password' name='password'>
                  </td>
               </tr>

                             <tr>
                  <td>
                  repeat your password:
                  </td>
                  <td>
                   <input type='password' name='repeatpassword'>
                  </td>
               </tr>
               
               <tr>
                  <td>
                  Email Address:
                  </td>
                  <td>
                   <input type='text' name='email'>
                  </td>
               </tr>

               </table>
               <p>
               <input type='submit' name='submit' value='Register'>
</html>

 

 

Can someone tell me whats wrong? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/178642-solved-notice-undefined-index/
Share on other sites

that happens because you try to set variables to POST data that doesn't exist yet. you should do something like

 

if (isset($_POST['submit'])){
$submit = $_POST['submit'];
$fullname = strip_tags($_POST['fullname']);
$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = ("date.America/New_York");
$email = $_POST['email'];
//etc.

 

What happens is you try to access the $_POST array with indexs that don't exist yet. Which is why the error says undefined index

thats probably because you took what I posted and just slapped it in the middle of your code, instead of changing your code to model what I posted.

 

If i were to guess (and this is just a guess because you haven't posted your updated code) assuming you just copy pasted what I typed, you are missing the closing curly brace ('}') to end the if statement

Here is what I did:

 

//form data
if (isset($_POST['submit']))
{
$submit = $_POST['submit'];
$fullname = strip_tags($_POST['fullname']);
$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = ("date.America/New_York");
$email = $_POST['email'];
}

 

Do I have to do that for every one of the variables? (the 'isset' part)

Alright here is the updated code:

 

<?php
echo "<h1>Register</h1>";
//form data
if (isset($_POST['submit']))
{
$submit = $_POST['submit'];
$fullname = strip_tags($_POST['fullname']);
$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = ("date.America/New_York");
$email = $_POST['email'];

if ($submit)
{

   //open database
   $connect = mysql_connect("-","-","-");
   mysql_select_db("-"); //select database

   $namecheck = mysql_query("SELECT username FROM users WHERE username='$username'");
   $count = mysql_num_rows($namecheck);

   if ($count!=0)
   {
    die("Username Taken.");
   }

//check for existance
if ($fullname&&$username&&$password&&$repeatpassword)
  {

    if ($password==$repeatpassword)

    {
     //check char length of username and fullname
     if (strlen($username)>25||strlen($fullname)>25)
     {
      echo "Length of username is too long!";
     }
     else
     {

      //check password length
      if (strlen($password)>25||strlen(password)<6)
      {
       echo "Password must be between 6 and 25 characters.";
      }
      else
      {
       //register user!
       
       //encrypt password
    $password = md5($password);
    $repeatpassword = md5($repeatpassword);


       //generate random number for activation proccess
       
       $random = rand(23456789,98765432);
      
      $queryreg = mysql_query("
      
      
      INSERT INTO users VALUES ('','$fullname','$username','$password','$email','$date','$random','0')

      ");
      
      die("You have been registered! <a href='http://www.zomvie.com/index.php'>Return to login page</a>");


      }

     }

    }
    else
        echo "Your passwords do not match.";
}
else
     echo "Please fill in <b>all</b> fields";
}


?>

<html>
<p>
<form action='http://www.zomvie.com/register.php' method='POST'>
       <table>
              <tr>
                  <td>
                  Your full name:
                  </td>
                  <td>
                   <input type='text' name='fullname' value='<?php echo $fullname; ?>'>
                  </td>
               </tr>

                             <tr>
                  <td>
                  Choose a username:
                  </td>
                  <td>
                   <input type='text' name='username'value='<?php echo $username; ?>'>
                  </td>
               </tr>

                             <tr>
                  <td>
                  Choose a password:
                  </td>
                  <td>
                   <input type='password' name='password'>
                  </td>
               </tr>

                             <tr>
                  <td>
                  repeat your password:
                  </td>
                  <td>
                   <input type='password' name='repeatpassword'>
                  </td>
               </tr>
               
               <tr>
                  <td>
                  Email Address:
                  </td>
                  <td>
                   <input type='text' name='email'>
                  </td>
               </tr>

               </table>
               <p>
               <input type='submit' name='submit' value='Register'>
</html>

 

 

now when i refresh the page most of it is gone but I get this:

 

error2g.jpg

ok.. yeah as I thougth you just copy pasted what I posted at the top of your code.

 

this line

if (isset($_POST['submit']))

 

was meant to replace this line

if ($submit)

 

since essentially they do the same thing, and this stuff,

$submit = $_POST['submit'];
$fullname = strip_tags($_POST['fullname']);
$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = ("date.America/New_York");
$email = $_POST['email'];

 

should go after the isset test:

if (isset($_POST['submit']))

 

but if you want to fix your code the easy way, you seem to be missing  a closing curly bracket at the end of your nested if/elses. just add one after this bit

 

}
else
     echo "Please fill in <b>all</b> fields";
}

 

either way it will do the same thing so it doesn't matter much

 

Alright just one last thing. on the inside of my Full Name box it says:

 

<br /> <b>Notice</b>:  Undefined variable: fullname in <b>/www/zxq.net/j/r/e/jreed2236/htdocs/register.php</b> on line <b>97</b><br />

 

and on the inside of my choose a username box it says:

 

<br /> <b>Notice</b>:  Undefined variable: username in <b>/www/zxq.net/j/r/e/jreed2236/htdocs/register.php</b> on line <b>106</b><br />

 

other than that everything is fine..any suggestions?

 

<?php
echo "<h1>Register</h1>";
//form data



if (isset($_POST['submit']))
{
$submit = $_POST['submit'];
$fullname = strip_tags($_POST['fullname']);
$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = ("date.America/New_York");
$email = $_POST['email'];
{

   //open database
   $connect = mysql_connect("-","-","-");
   mysql_select_db("-"); //select database

   $namecheck = mysql_query("SELECT username FROM users WHERE username='$username'");
   $count = mysql_num_rows($namecheck);

   if ($count!=0)
   {
    die("Username Taken.");
   }

//check for existance
if ($fullname&&$username&&$password&&$repeatpassword)
  {

    if ($password==$repeatpassword)

    {
     //check char length of username and fullname
     if (strlen($username)>25||strlen($fullname)>25)
     {
      echo "Length of username is too long!";
     }
     else
     {

      //check password length
      if (strlen($password)>25||strlen(password)<6)
      {
       echo "Password must be between 6 and 25 characters.";
      }
      else
      {
       //register user!
       
       //encrypt password
    $password = md5($password);
    $repeatpassword = md5($repeatpassword);


       //generate random number for activation proccess
       
       $random = rand(23456789,98765432);
      
      $queryreg = mysql_query("
      
      
      INSERT INTO users VALUES ('','$fullname','$username','$password','$email','$date','$random','0')

      ");
      
      die("You have been registered! <a href='http://www.zomvie.com/index.php'>Return to login page</a>");


      }

     }

    }
    else
        echo "Your passwords do not match.";
}
else
     echo "Please fill in <b>all</b> fields";
}
}

?>

<html>
<p>
<form action='http://www.zomvie.com/register.php' method='POST'>
       <table>
              <tr>
                  <td>
                  Your full name:
                  </td>
                  <td>
                   <input type='text' name='fullname' value='<?php echo $fullname; ?>'>
                  </td>
               </tr>

                             <tr>
                  <td>
                  Choose a username:
                  </td>
                  <td>
                   <input type='text' name='username'value='<?php echo $username; ?>'>
                  </td>
               </tr>

                             <tr>
                  <td>
                  Choose a password:
                  </td>
                  <td>
                   <input type='password' name='password'>
                  </td>
               </tr>

                             <tr>
                  <td>
                  repeat your password:
                  </td>
                  <td>
                   <input type='password' name='repeatpassword'>
                  </td>
               </tr>
               
               <tr>
                  <td>
                  Email Address:
                  </td>
                  <td>
                   <input type='text' name='email'>
                  </td>
               </tr>

               </table>
               <p>
               <input type='submit' name='submit' value='Register'>
</html>

that happens because you don't define either of those variables unless you submit the form, because of the following:

 

if (isset($_POST['submit']))

 

why would you want to populate the form with something that would be empty anyways? just remove the  <?php echo $username; ?> and <?php echo $fullname; ?> stuff.

 

but one thing i noticed

 

if (isset($_POST['submit']))
{
$submit = $_POST['submit'];
$fullname = strip_tags($_POST['fullname']);
$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = ("date.America/New_York");
$email = $_POST['email'];
{//this???

that opening curly brace doesn't seem to need to be there. Honestly, I'm surprised that you don't get a syntax error

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.