index.php

```
<!DOCTYPE HTML>  
   <html>
   <head>
       <style>
       .error {
        color:red;
       }
      </style>
  </head>
  <body>  

<?php
// define variables and set to empty values
include_once 'connect.php';
$nameErr = $emailErr = $usernameErr = $passwordErr = $DateOfBirthErr = $departmentErr = $ageErr = "";
$name    = $email = $username = $password = $DateOfBirth = $department = $age = "";

if (isset($_POST['submit'])) {
    if (empty($_POST["name"])) {
        $nameErr = "Name is required";
    } else {
        $name = test_input($_POST["name"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
            $nameErr = "Only letters and white space allowed";
        }
    }

    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email format";
        }
    }

    if (empty($_POST["username"])) {
        $usernameErr = "username is required";
    } else {
        $username = test_input($_POST["username"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/", $username)) {
            $usernameErr = "Only letters and white space allowed";
        }
    }

    if (empty($_POST["password"])) {
        $passwordErr = "password is required";
    } else {
        $password        = test_input($_POST["password"]);
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);
        // check weather password is alphanumeric
        if (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{6,}$/', $password)) {
            $passwordErr = "Password must be alphanumeric and atleast 6 characters 
    long!";
        }
    }

    if (empty($_POST["Date_of_birth"])) {
        $DateOfBirthErr = "Date Of Birth is required";
    } else {
        $DateOfBirth = test_input($_POST["Date_of_birth"]);
    }

    if (empty($_POST["department"])) {
        $departmentErr = "Department is required";
    } else {
        $department = test_input($_POST["department"]);
    }

    if (empty($_POST["age"])) {
        $ageErr = "AGE is required";
    } else {
        $age = test_input($_POST["age"]);
    }

    if ($nameErr == "" && $emailErr == "" && $usernameErr == "" && $passwordErr == "") {
        $check = "SELECT * FROM users WHERE username = '$_POST[username]'";
        $rs    = mysqli_query($mysqli, $check);
        $da    = mysqli_fetch_array($rs, MYSQLI_NUM);
        if ($da[0] > 1) {
            echo "Username Already in Exists<br/>";
        }

        else {
            $sql = "INSERT INTO users(`id`,`username`, `password`, `email` , `name` , 
   `Date_of_birth` , `department` ,`age`)
         VALUES ('','" . $username . "', '" . $hashed_password . "', '" . $email . "' , 
    '" . $name . "' , '" . $DateOfBirth . "' , '" . $department . "' , '" . $age . "')";

            if (mysqli_query($mysqli, $sql)) {
                echo "Registered successfully";
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($mysqli);
            }
            mysqli_close($mysqli);
        }
    }

}

function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>

       <div style="padding-left: 250px">
    <h2>Registration Form</h2>
     <p><span class="error">All fields are required </span></p>
    <form method="post" action="">  
     Name: 
   <input type="text" name="name" style="margin-left: 52px">
    <span class="error"> <?php echo $nameErr;?></span>
     <br><br>
    E-mail:
    <input type="text" name="email" style="margin-left: 48px">
     <span class="error"><?php echo $emailErr;?></span>
   <br><br>
  Username:
   <input type="text" name="username" style="margin-left:26px">
    <span class="error"> <?php echo $usernameErr;?></span>
   <br><br>
   Password:
 <input type="password" name="password" style="margin-left:30px">
   <span class="error"> <?php echo $passwordErr;?></span>
  <br><br>
   Date Of Birth :
  <input type="date" name="Date_of_birth">
 <span class="error"> <?php echo $DateOfBirthErr;?></span>
      <br><br>
       Age :
     <input type="number" name="age" style="margin-left:62px">
 <span class="error"> <?php echo $ageErr;?></span>
   <br><br>
  Department :
   <select name="department" style="margin-left:14px">
     <option value="EE">Electrical & Electronics</option>
 <option value="EC">Electronics & Communication</option>
 <option value="ME">Mechanical</option>
     <option value="CS">Computer Science</option>
 <option value="CV">Civil</option>
    <option value="IS">Information Science</option>
  </select>
   <span class="error"> <?php echo $departmentErr;?></span>
    <br><br>
 <input type="submit" name="submit" value="Register">  
 </form>
 </div>
  </body>
 </html>

```

connect.php

```
<?php

$databaseHost     = 'localhost';
$databaseName     = 'amith';
$databaseUsername = 'root';
$databasePassword = '';

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);

?>

```

i'm creating a simple php registration form, i only have one issue which is not getting fixed i.e., when any one while registering enters the same username then an error message should throw saying that username already taken i have tried with the above code but its not working. please can any one help me to fix my issue.