Undefined variable problem in php login system/unity
P粉322106755
P粉322106755 2024-03-29 21:16:04
0
1
412

I get this error: Warning: Variable $loginUser is not defined in C:\xampp\htdocs\BNP\Login.php on line 21 Username does not exist. I tried other fixes but it returns the same error message. Can anyone help?

<?php

//variables for connecting to DB
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "unityaccess";

//variables submitted by user
if(isset($_POST['loginUser'])){ $loginUser = $_POST['loginUser']; }
if(isset($_POST['loginPass'])){ $loginPass = $_POST['loginPass']; }

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT password FROM users WHERE username = '" . $loginUser . "'";


$result = $conn->query($sql);

if ($result->num_rows > 0){
  // output data of each row
  while($row = $result->fetch_assoc()) {
if($row["password"] == $loginPass){
    echo "Login Success, welcome.";
}
else {
    echo "Wrong Credentials";
}
  }
} else {
  echo "Username does not exists";
}

$conn = null;



?>

Thanks

P粉322106755
P粉322106755

reply all(1)
P粉118698740

Your $loginUser is not defined, also check if your form name attribute has "loginUser" and use this updated code where I have defined $loginUser and it will work.

connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "SELECT password FROM users WHERE username = '" . $loginUser . "'";
    
    
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0){
      // output data of each row
      while($row = $result->fetch_assoc()) {
    if($row["password"] == $loginPass){
        echo "Login Success, welcome.";
    }
    else {
        echo "Wrong Credentials";
    }
      }
    } else {
      echo "Username does not exists";
    }
    
    $conn = null;
    
    
    
    ?>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template