Undefined Index in Sign up form

So I am attempting to create a simple sign up form, just with Username and Email as I’m just testing things, and I keep getting the following error:

_Notice: Undefined index: username in C:\xampp\htdocs\tes\createacc.php on line 5

Notice: Undefined index: email in C:\xampp\htdocs\tes\createacc.php on line 6_

This is my code:

<?php
session_start();
require_once 'dbc.php';
if(isset($_POST['submit'])) {
 $username = strip_tags($_POST['username']);
 $email = strip_tags($_POST['email']);
 $username = $DBcon->real_escape_string($username);
 $email = $DBcon->real_escape_string($email);
 $check_username = $DBcon->query("SELECT username FROM users WHERE username='$username'");
 $count=$check_username->num_rows;
 if ($count==0) {
  $query = "INSERT INTO users(username,email) VALUES('$username','$email')";
  if ($DBcon->query($query)) {
   echo "Created";
  }else {
   echo "Error";
  }
 } else {
  echo "In-use";
 }
 $DBcon->close();
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Login</title>
        <link href="/fonts" rel="stylesheet">
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
        <div class="login-page">
            <div class="form">
                <form class="registration-form">
                <form action="" method="post">
                    <input type="text" name="username" id="username" placeholder="Username"/>
                </form>
                <form action="" method="post">
                    <input type="password" name="password" id="password" placeholder="Password"/>
                </form>
                <form action="" method="post">
                    <input type="text" name="email" id="email" placeholder="Email Address"/>
                </form>
                <form action="" method="post">
                    <input type="submit" name="submit" id="submit" value="Create Account">
                </form>
                <p class="message">Already registered? <a href="login.php">Sign In</a></p>
                </form>
            </div>
            <center><i><font size="1">Copyright (C) 2017 - Please read <a href="tou.php">Terms of Use</a>.</i></center>
        </div>
        <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
        <script src="js/index.js"></script>
    </body>
</html>

I’m still new, so sorry for any simple mistakes.

Your form tags are a bit strange - you keep opening and closing forms all over the shop. As far as I’m aware, you open the form once before your first input, and close it just after your last input. As you separate each input into a separate form, I would think that the problem is because the form that contains the submit does not contain any of the other fields, hence the error message.

1 Like

Take a look at the html you are using for the form. There should be only a <form action="" method="post"> at the beginning of the form and a closing </form> at the end of the form. Inside the form, there should not be any form tags, because then you are starting and stopping numerous different forms.

Okay, @droopsnoot beat me by about 5 seconds there. :slight_smile: But at least I managed to sneak in ahead of @SamA74.

1 Like

You appear to have several form elements in your html. I think there should only be one containing all of the inputs.
Please validate your html.

1 Like

@WebMachine @SamA74 Thank you guys, looks like my issue was not with my PHP, but with my HTML form tags. Thanks a lot :slight_smile:

Undefined Index generally means that the field you tried to reference does not exist. As everyone has said, you separated the forms so the first form is what gets submitted. In which case, the only thing that gets submitted is the username. You should also consider using prepared statements because you are wide open for SQL Injections.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.