Php session, header, and password hash values

i’m currently testing md5 not to store passwords in plain text.
i’m working with 2 files, the first one registers users and inserts the md5 of the password in the database.
The 2nd file is login.php where i attempt user login, comparing the hash value of the password typed with the hash value of the one that is stored,
after testing that the 2 values are equal with echos, the header redirecting isn’t working, here’s my code for login.php if anyone can give me some tips :



      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

 $sqlx = "SELECT passcode FROM jdashboard WHERE username = '".$myusername."'";
 $resultx = mysqli_query($db,$sqlx);
 $rowx = mysqli_fetch_array($resultx,MYSQLI_ASSOC);

$enc_password = md5($mypassword);
/*echo $enc_password;
echo "<br>";
echo $storedpass;*/

      $sql = "SELECT id FROM jdashboard WHERE username = '$myusername' and passcode = '$mypassword'";

      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
        $_SESSION['myusername'] = 'myusername';
$sql = "SELECT admin FROM jdashboard WHERE username = '".$myusername."'";
      $result = mysqli_query($db,$sql);
  $row = mysqli_fetch_array($result,MYSQLI_ASSOC);

$admin= $row['admin'];

         $_SESSION['login_user'] = $myusername;

         if ($admin=='1'){

         header("location: welcome.php");


 else if ($admin=='0' && $storedpass == $enc_password){


      else {
         $error = "Your Login Name or Password is invalid";
      } }

Thank you in advance !

Your code is wrong here

$sql = "SELECT id FROM jdashboard WHERE username = '$myusername' and passcode = '$mypassword'";

You should compare $enc_password instead of $mypassword, so the right query should like this.

$sql = "SELECT id FROM jdashboard WHERE username = '$myusername' and passcode = '$enc_password'";

Anyway, you shouldn’t use md5 as encrypted password dual to the security issues, I would suggest that you can use Bcrypt PHP Class instead

Hope this help.