I am trying to insert data on my database, but when i do, it says UNDEFINED INDEX

HTML code here

<form  action="insert.php" autocomplete="on"> 
        <h1> Sign up </h1> 
                                <p> 
                                    <label for="username">Your username</label>
                                    <input id="username" name="username" type="text" />
                                </p>
                                <p> 
                                    <label for="emails"  > Your email</label>
                                    <input id="emails" name="emails" type="email" /> 
                                </p>
                                <p> 
                                    <label for="password" >Your password </label>
                                    <input id="password" name="password"  type="password" />
                                </p>
                                <p> 
                                    <label for="passwordcon" >Please confirm your password </label>
                                    <input id="passwordcon" name="passwordcon"  type="password" />
                                </p>
                                <p class="signin button"> 
                                    <input type="submit" value="Sign up"/> 
                                </p>

                            </form>

PHP Code Here

<?php
$con=mysqli_connect("localhost","root","","database");

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


$username = mysqli_real_escape_string($con, $_POST['username']);
$emails = mysqli_real_escape_string($con, $_POST['emails']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$passwordcon = mysqli_real_escape_string($con, $_POST['passwordcon']);
$sql="INSERT INTO users (usernamesignup, emailsignup, passwordsignup, passwordsignup_confirm)
VALUES ('$username', '$emails', '$password', '$passwordcon')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>

thank you in advance..

Dani AI

Generated

Short answer: the notices came from trying to read $_POST keys when the form never sent them. As pointed out, the form was missing the POST method; adding the form attribute to submit with POST fixes the immediate "Undefined index" error and is what ultimately discovered.

A few practical points and hardening steps to prevent the same problem or future bugs:

  • Only read $_POST[...] after confirming a POST request. For example, wrap processing in a request-method check and use isset() or the null-coalescing operator to avoid notices when a field is missing. Also confirm your form name attributes exactly match the keys you read server-side.
  • Validate inputs before using them (e.g., filter_var for email) and check password matches the confirmation client/server-side. You generally should not keep the confirmation value in the database — only store the final (hashed) password.
  • Never insert raw values into SQL strings. Use prepared statements (mysqli or PDO) and use password_hash() to store passwords; verify with password_verify() on login.

Example (conceptual) pattern to follow:

// validate request, sanitize/validate, hash password, then use a prepared statement
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // get and validate fields, check passwords match, $hash = password_hash($password, PASSWORD_DEFAULT);
    // prepare and execute INSERT with bound parameters (mysqli or PDO)
}

Extra tips: enable full error reporting while developing (error_reporting(E_ALL); ini_set('display_errors',1);), var_dump($_POST) to inspect incoming data, and redirect after successful insert to avoid duplicate submissions. These steps will eliminate the undefined-index notices and make the code safer and more robust.

i managed to fixed it. lol

well, I'm late ... it should be:

<form action="insert.php" method="post" autocomplete="on"> 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.