-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>
Community
  • 1
  • 1
user1371500
  • 93
  • 2
  • 4
  • 10
  • 7
    Please stop writing new code with the ancient `mysql_*` functions. They are no longer maintained and community has begun the [deprecation process](http://goo.gl/KJveJ) . Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you cannot decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, [here is a good PDO-related tutorial](http://goo.gl/vFWnC). – vascowhite May 13 '12 at 23:27
  • Check the solution to the linked question, it will probably help. – vascowhite May 13 '12 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. – David May 13 '12 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` – Sampo Sarrala - codidact.org May 13 '12 at 23:35
  • You can set empty value to form action insted of `$_SERVER['PHP_SELF']` – Gabriel Santos May 13 '12 at 23:59

1 Answers1

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!

Kneel-Before-ZOD
  • 4,141
  • 1
  • 24
  • 26
  • correct me if i'm wrong, but wouldn't `if ($_POST["username"])` be better ? – Sliq May 14 '12 at 01:12
  • 1
    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 – Kneel-Before-ZOD May 14 '12 at 02:43