-2

Possible Duplicate:
strange php error
Submit an HTML form with empty checkboxes
PHP form checkbox and undefined index

Some reason I am getting undefined index errors in the following code. Any help would be appreciated. Here are the errors I am Recieving

Notice: Undefined index: username in C:\xampp\htdocs\register.php on line 5
Notice: Undefined index: password in C:\xampp\htdocs\register.php on line 6
Notice: Undefined index: firstname in C:\xampp\htdocs\register.php on line 7
Notice: Undefined index: surname in C:\xampp\htdocs\register.php on line 8

Code:

<?php
mysql_connect("localhost", "root", "") or die(mysql_error()); 

 mysql_select_db("clubresults") or die(mysql_error()); 
   $username = $_POST['username'];
   $password = md5($_POST['password']);
   $firstname = $_POST['firstname'];
   $surname = $_POST['surname'];

   $sql = "INSERT INTO members (Username, Password, Firstname, Surname) VALUES ($username, $password, $firstname, $surname);";

   mysql_query($sql);

?>

<html>
        <div class="content">
        <center>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
             <table border="0">
             <tr><td colspan=2><h1>Register</h1></td></tr> 
             <tr><td>Username:</td><td>
             <input type="text" name="username" maxlength="60">
             </td></tr>
             <tr><td>Password:</td><td>
             <input type="password" name="password" maxlength="10">
             </td></tr>
             <tr><td>Firstname:</td><td>
             <input type="text" name="firstname" maxlength=210">
             </td></tr>
             <tr><td>Surname:</td><td>
             <input type="text" name="surname" maxlength=210">
             </td></tr>
             <tr><th colspan=2><input type="submit" name="submit" 
            value="Register"> </th></tr> </table>
         </form>
         </center>
         </div>
</html>
5
  • 7
    Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented May 13, 2012 at 23:27
  • Check the solution to the linked question, it will probably help. Commented May 13, 2012 at 23:30
  • 1
    Your code assumes those values are present. When the form is posted, they will be. But when the page is initially loaded, they won't be. Look into the isset() function to check if a value in an array exists before trying to use that value. Commented May 13, 2012 at 23:33
  • One thing that i dont like is url shortening where it is not needed, other is asking about sql while problems is with arrays. Revise your $_POST Commented May 13, 2012 at 23:35
  • You can set empty value to form action insted of $_SERVER['PHP_SELF'] Commented May 13, 2012 at 23:59

1 Answer 1

2

When your page loads for the first time (unless it was the action of another page), the REQUEST_METHOD is 'GET'; as a result, no POST variables exist (although the $_POST superglobal exists).
When you POST, either from another page, or from the same page, all the data set in the form will be present in the $_POST superglobal, and can be accessed the way you accessed them earlier; unfortunately, if a data is not present, and you try to access it, an error will be thrown, similar to what you have.
To solve the issue, especially if you want to post to the same page as the form, use something like

  if (isset($_POST["username"]))
   {
      // do whatever
   }
  if (isset($_POST["password"]))
   {
      // do whatever
   }
  if (isset($_POST["firstname"]))
   {
      // do whatever
   }
  if (isset($_POST["surname"]))
   {
      // do whatever
   }

With that, your code won't throw an error if those data aren't present.
Hope that helps.
On an unrelated, but important note, do not use mysql_* functions anymore. They have been deprecated; use mysqli or PDO functions for data access.


Happy coding!

Sign up to request clarification or add additional context in comments.

2 Comments

correct me if i'm wrong, but wouldn't if ($_POST["username"]) be better ?
No, because you are assuming that $_POST["username"] will be available; error will still be thrown if username isn't available. The isset($_POST["username"]) prevents error from being thrown

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.