首页 > 解决方案 > 在登录中的PHP“未定义变量”错误

问题描述

当我登录系统时,我得到一个错误。我尝试使用“电子邮件”而不是 id 来修复它,但它不起作用。我是 PHP 新手。我该如何解决这个问题?

注意:未定义变量:第 161 行 C:\xampp\xAmmp\htdocs\project\index.php 中的 id

索引.php

...
<?php if ($id == $_SESSION['id'] ) { ?>
            <h4>
                <span style="font-family:\'Pacifico\', cursive;">Welcome, </span>
                <div class="btn-group">
                    <button type="button" class="btn btn-success dropdown-toggle"
                            data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <i class="glyphicon glyphicon-user"></i> <?php echo explode(' ', $_SESSION["name"])[0] ?> <span
                            class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
...

登录.php

<?php

include('conn.php');

session_start();
$error = "";

//email entered by user
$email = mysqli_real_escape_string($link, $_POST['email']);

//password entered by user md5 method is used for password encryption
$password = md5(md5($_POST['email']) . $_POST['password']);

//Query to see whether user and password combination exists
$query = "SELECT * FROM users WHERE email = '$email' AND password = '$password' LIMIT 1";

if ($result = mysqli_query($link, $query)) {
    if ($row = mysqli_fetch_array($result)) {

            $_SESSION['name'] = $row['name'];
            $_SESSION['email'] = $row['email'];
            $_SESSION['dob'] = $row['dob'];
            if (isset($row['rest']))
                $_SESSION['rest'] = $row['rest'];
        } else {
        $error .= "User does not exists with that combination of email and password.<br />";
    }
} else {
    $error .= "User does not exists with that combination of email and password.<br />";
}

if ($error != "")
    echo '<div class="alert alert-danger alert-dismissible fade in" role="alert" style="margin-top: 20px">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                ' . $error . '</div>';
?>

标签: php

解决方案


推荐阅读