0

PHP

if($count == 1) {
     session_register("username");
     $_SESSION['username'] = $username;

     header("location: ../home");
  }else {
     $error = "Your Login Name or Password is invalid";
  }

HTML -- line 43

<?php echo "<p class='text-danger'>$error</p>"; ?>

Notice: Undefined variable: error in directory on line 43

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
Mark
  • 11
  • 1

2 Answers2

2

Error occurred because, when if statement is true then only under if statement block code would be executed not else block code. So $error variable did not initialized. Each variable should be initialized if you want to use it. $error variable did not initialize but you use it in HTML-- line 43. Thanks

  $error ="";
  if($count == 1) {
     session_register("username");
     $_SESSION['username'] = $username;

     header("location: ../home");
  }else {
     $error = "Your Login Name or Password is invalid";
  }
Didarul Alam
  • 79
  • 2
  • 18
0
$error = '';

if($count == 1) {
     session_register("username");
     $_SESSION['username'] = $username;

     header("location: ../home");
  }else {
     $error = "Your Login Name or Password is invalid";
  }
Vinod Kirte
  • 189
  • 1
  • 6
  • 2
    Why should the OP "use this code"? A **good answer** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer. – RiggsFolly Jan 21 '17 at 12:30
  • I defined the variable before if statement, same error – Mark Jan 21 '17 at 12:52