在 POST 表单之后的 php 脚本中未定义的索引


Undefined index in php script after a POST form

$username = $_POST['username'];
$ppassword = $_POST['password'];
$password = md5("$ppassword");

这是代码,我收到此错误

Notice: Undefined index: username in C:'wamp'www'php'compare.php on line 14
Call Stack
#   Time    Memory  Function    Location
1   0.0004  250440  {main}( )   ..'lognet.php:0
2   0.0007  260000  include( 'C:'wamp'www'php'compare.php' )    ..'lognet.php:3

Notice: Undefined index: password in C:'wamp'www'php'compare.php on line 15
Call Stack
#   Time    Memory  Function    Location
1   0.0004  250440  {main}( )   ..'lognet.php:0
2   0.0007  260000  include( 'C:'wamp'www'php'compare.php' )    ..'lognet.php:3

这是表格:

<form name="form1" method="post" action="compare.php">
<strong><input id="close" type="button" value="X"                                     
onclick="getElementById('loginbox').style.display = 'none'">Member Login</strong></br>
Username:<input name="username" type="text" id="username"></br>
Password:<input name="password" type="password" id="password">
&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;</br>
<input type="submit" name="Submit" value="Login">
</form>

我让它工作,但是当他们将 php 更新到 5.4.12 时,它停止了,我不得不更改它。

你可以

做这样的事情——

$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? md5($_POST['password']) : '';

使用 isset() 函数来检查变量集与否?

$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? md5($_POST['password']) : '';

你的 html 代码放到这里,确保你的 html 文本字段名称应该与 $_POST['variable_name'] 匹配

例如,

如果您在 html 表单的同一页面中有此代码,则需要以下内容:

if(isset($_POST['password']) && isset($_POST['username'])){
$username = $_POST['username'];
$ppassword = $_POST['password'];
$password = md5("$ppassword");
}

如果您在其他页面中有此代码,请确认您是否收到带有

echo $_POST['username'];
echo $_POST['password'];

如果不是,则输入错误

<form action="somepage.php" method="post">
<input type="text" name="username" value="" />
<input type="password" name="password" value="" />
<input type="submit" name="submit" value="Submit" />
</form>