Home  >  Q&A  >  body text

Undefined variable problem in php login system/unity

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粉322106755P粉322106755227 days ago396

reply all(1)I'll reply

  • P粉118698740

    P粉1186987402024-03-30 20:33:44

    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;
        
        
        
        ?>

    reply
    0
  • Cancelreply