首页  >  问答  >  正文

php登录系统/unity中的未定义变量问题

我收到此错误:警告:第 21 行 C:\xampp\htdocs\BNP\Login.php 中未定义变量 $loginUser 用户名不存在。我尝试了其他修复,但它返回相同的错误消息。有人可以帮忙吗?

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



?>

谢谢

P粉322106755P粉322106755227 天前400

全部回复(1)我来回复

  • P粉118698740

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

    您的 $loginUser 未定义,还要检查您的表单名称属性是否有“loginUser”,并使用此更新的代码,我在其中定义了 $loginUser ,它将起作用。

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

    回复
    0
  • 取消回复