Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8046147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:43:08+00:00 2026-06-05T05:43:08+00:00

I have a little issue with my login form. I think the problem is

  • 0

I have a little issue with my login form. I think the problem is mysqli code because when the code was old mysql code, the login worked correctly but since I changed it to mysqli, it has not quite worked properly.

What is suppose to happen is that the user will enter in their username and password in the login form, when the user clicks on the “Login” button, it will check in the database if the username and password is correct. If it is correct then navigate to the menu.php page else if login is incorrect, display a message stating log in is incorrect, try again.

Instead what the code below is doing is that when the user enters in their username and password and clicks on “Login” button, no matter if username and password is correct or not it just refreshes the from, it doesn’t navigate to menu.php page or display a login incorrect message.

So my question is that why is this happening, why after logging in does it not navigate the user or display the incorrect login message?

Below is the code:

 <?php

    session_start(); 

    $username="xxx";
    $password="xxx";
    $database="xxx";

    $mysqli = new mysqli("localhost", $username, $password, $database)or die( "Unable to select database");

    foreach (array('teacherusername','teacherpassword') as $varname) {
            $$varname = (isset($_POST[$varname])) ? $_POST[$varname] : '';
          }

    ?>

    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" id="teachLoginForm">        
    <p>Username</p><p><input type="text" name="teacherusername" /></p>      <!-- Enter Teacher Username-->
    <p>Password</p><p><input type="password" name="teacherpassword" /></p>  <!-- Enter Teacher Password--> 
    <p><input id="loginSubmit" type="submit" value="Login" name="submit" /></p>
    </form>

    <?php
    if (isset($_POST['submit'])) {

    $query = $mysqli->prepare("
    SELECT * FROM Teacher t  
    WHERE 
    (t.TeacherUsername = '".mysqli_real_escape_string($teacherusername)."')
    AND
    (t.TeacherPassword = '".mysqli_real_escape_string($teacherpassword)."')
    ");

    $query->bind_result($Teacher);

    $num = $query->num_rows($result = $query->execute());

    $loged = false;

    while($row = $result->fetch())
      {

          if ($_POST['teacherusername'] == ($row['TeacherUsername']) && $_POST['teacherpassword'] == ($row['TeacherPassword']))
          {
              $loged = true;
          }

$_SESSION['teacherforename'] = $row['TeacherForename'];
$_SESSION['teachersurname'] = $row['TeacherSurname'];
$_SESSION['teacherusername'] = $row['TeacherUsername'];

      }

      if ($loged == true){
      header( 'Location: menu.php' ) ;
    }else{
      echo "The Username or Password that you Entered is not Valid. Try Entering it Again.";
    }


    }
     ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:43:09+00:00Added an answer on June 5, 2026 at 5:43 am

    This code is really bad for a number of reasons. Let me try to rewrite this for you and explain why.

    First off, you’re getting and looping through all of the rows of the database and checking for a match, you should be doing this within your SQL query.

    Second, you’re trying to send headers after output is sent. This won’t work and should throw an error if you have error reporting enabled.

    Third, you define the variables $teacherusername and $teacherpassword for no real reason, and don’t even consistently use them.

    Fourth, you’re starting a session for seemingly no reason.

    Fifth, your query is unnecessarily complicated.

    Sixth, as pointed out by Mike, you’re storing users passwords in the database as plaintext.

    I’d write this as:

    <?php
    // There can be nothing (HTML, whitespace, anything) above this php tag.
    
    if(isset($_POST['teacherusername']) && isset($_POST['teacherpassword']))
    {
        // Connect to Database
        $username="xxx";
        $password="xxx";
        $database="xxx";
        $mysqli = new mysqli("localhost", $username, $password, $database)or die( "Unable to select database");
    
        // Build Query
        $q = sprintf('SELECT * FROM Teacher t');
        $q.= sprintf(' WHERE t.TeacherUsername = %s', mysqli_real_escape_string($_POST['teacherusername']));
        $q.= sprintf(' AND t.TeacherPassword = %s', mysqli_real_escape_string($_POST['teacherpassword']));
    
        // Run Query
        $query->bind_result($Teacher);
        $res = $query->execute();
        $num = $query->num_rows();
    
        if($num == 1)
        {
            header('Location: menu.php' );
        }
        else
        {
            session_start();        // Do you need this?
            echo "The Username or Password that you Entered is not Valid. Try Entering it Again.";
        }
    }
    ?>
    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" id="teachLoginForm">        
    <p>Username</p><p><input type="text" name="teacherusername" /></p>      <!-- Enter Teacher Username-->
    <p>Password</p><p><input type="password" name="teacherpassword" /></p>  <!-- Enter Teacher Password--> 
    <p><input id="loginSubmit" type="submit" value="Login" name="submit" /></p>
    </form>
    

    Now, this doesn’t resolve the fact that you’re still storing passwords in the database as plaintext. That’s bad practice, but kind of outside the scope of today’s lesson 🙂

    Here, the PHP is run first, and either header() or session_start() is the first thing that sends output — this is important as they both need to be the first thing to send output.

    I also resolved you’re validation loop. If the query returns rows then there are matches in the database and the username and password is correct, no need to loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little problem with javascript form submit issue, here is the script
I have a little issue concerning CSRF security and a login form. The form
I have a little issue with Java UTF-8 converting.I have this Objective C code
I have a little issue with Android SQL queries. Here is the my code
I have a little issue concerning an animation-effect which loads a certain div into
Hey guys I have a little issue here. I have a panel where I
im having a little issue here. I have MediaElement.js set on my project. It
I have little bit longer question for you - but hope answer will be
I have little snippet for validatin' my form. I need help to position the
I have a little architecture issue. On my site user can register and when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.