I am doing Perl and HTML programming with two files: `mainpage.html` and `mainpage.cgi`. I want let users input their username and password in text boxes, and if either of them is wrong, the page should stay the same and let user type again.

### mainpage.html

```
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>User Login</title>
  </head>
  <body>
    <form action="http://localhost/cgi-bin/mainpage.cgi" method="post">
      Username: <input type="text" name="Username">  <br />
      Password: <input type="text" name="Password" />
      <input type="submit" value="submit" />
    </form>
  </body>
</html>

```

### mainpage.cgi

```
#!c:/Perl64/bin/perl.exe

local ( $buffer, @pairs, $pair, $Username, $Password, %FORM );

# Read information
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;

if ( $ENV{'REQUEST_METHOD'} eq "POST" ) {
    read( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );
}
else {
    $buffer = $ENV{'QUERY_STRING'};
}

@pairs = split( /&/, $buffer );

foreach $pair ( @pairs ) {
    ( $Username, $Password ) = split( /=/, $pair );
    $Password =~ tr/+/ /;
    $Password =~ s/%(..)/pack("C", hex($1))/eg;
    $FORM{$Username} = $Password;
}
$Username = $FORM{Username};
$Password = $FORM{Password};

#Database
use DBI;
$dbh = DBI->connect( 'dbi:mysql:math_questions', 'root', 'admin' )
        or die "Connection Error: $DBI::errstr\n";

$sql = "select * FROM users where Username = '$Username' and Password = '$Password'";
$sth = $dbh->prepare( $sql );
$sth->execute or die "SQL Error: $DBI::errstr\n";
while ( @row = $sth->fetchrow_array ) {
    $user = $row[0];
    $key  = $row[1];
}

if ( $user ) {
    print "Content-type:text/html\r\n\r\n";
    print "<html>";
    print "<head>";
    print '<meta charset="utf-8">';
    print '<title>math_questions</title>';
    print "</head>";
    print "<body>";
    print "<h2>Correct</h2>";
    print "</body>";
    print "</html>";
}
else {
    #I don't know how to do this part in a good way, thank you very much!
}

1;

```