未定义变量:用户名和密码


Undefined variable: username and password

我知道这个问题一遍又一遍地发布,我已经尝试过用

修复它

if(isset($_POST['username'])){}…检查$_POST是否用大写字母,看看它们在登录表单和check_login脚本中是否声明正确,我只是找不到为什么会发生这种情况。

昨天一切都很好,今天当我打开我的电脑,我只是试着走得更远,突然它不再工作了,数据库表用户是空的吗?!我通过xampp将数据库存储在本地,所以没有人可以进入它,我不知道到底发生了什么变化。

这是我的登录表单:


<?php 
session_start();
if(isset($_SESSION['memberlogged'])== true)
{
    header("location: login_success.php");
}
?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<center>
<body style="background-color:rgb(248,248,248);">
<div id="loginmenu">
<img src="/img/choose.jpg"><br>
<input type="text" name="username" id="unu" placeholder="username"> <br /><br>
<input type="password" name="password" id="unu"  placeholder="password"> <br />
<br><input type="submit" value="Log in" id="button" onClick="parent.location='check_login.php'"> 
<div class="divider"></div>
<input type="submit" id="button2" onClick="parent.location='index.php'" value='Leave' name="leave">
</center>
</div>
</body>
</html>

这是我的check_login。php脚本


<?php
include ("config.php");
// username and password sent from form
$password=$_POST['password']; 
$username=$_POST['username']; 
// To protect MySQL injection 
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$query="SELECT * FROM users WHERE username='$username' and password='$password'";
$result=mysql_query($query);
// Mysql_num_row is counting table row
if($query)
{
$count=mysql_num_rows($result);}
else
{die ("something is not good");}
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
 // SAVE SESSION VARIABLES AND REDIRECT TO "login_success.php"
    $_SESSION['username'] = $username;
    $_SESSION['memberlogged'] = true;
    header("location:login_success.php");
    exit;
}
else {
echo "Wrong Username or Password";
}
?>

当我尝试连接时(登录数据100%正确),我得到

注意:未定义变量:username在*********'check_login.php的第6行

注意:未定义变量:密码在*********'check_login.php的第7行用户名或密码错误

需要添加form HTML和action属性…

<form action="check_login.php" method="POST">
<input type="text" name="username" id="unu" placeholder="username"> <br /><br>
<input type="password" name="password" id="unu"  placeholder="password"> <br />
<br><input type="submit" value="Log in" id="button"> 
<div class="divider"></div>
<a href="index.php" class="stlying_class">Leave</a>
</form>

告诉表单将$_POST[x]信息发送到哪里以及如何使用POST

你可以删除onClick="parent.location='check_login.php'",因为这只会改变页面而不发送任何信息。

我们可以把Submit留给HTML输入类型submut

正如你所指出的,"离开"按钮仍然会显示表单,所以我把它改成了一个超链接,这可以根据你的喜好进行编辑。

或者您可以将其添加为没有类型的input,并保留on-click事件!