当在表单中写入任何内容并单击Enter按钮时,没有错误,但当我写入用户名和密码时,错误显示我尝试echo "$_POST['username']";//如果用户名不匹配,打印用户名
错误:注意:未定义索引:用户名在..
注意:未定义索引:password in .
这是我的表单
<form action="2.php" method="post">
<table align="center">
<tr>
<td>Username</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Enter" />
</td>
</tr>
</table>
</form>
这是我的第二页
<?php
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$connection = mysql_connect('localhost', 'root', '');
if (!$connection){
die('Could not connect');
exit;
}
mysql_select_db('dbName') or die( "Unable to select database");
$query = "SELECT * FROM admin WHERE username = '$username'";
$result = mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($result); // number of rows
if ($num > 0){
$i = 0;
while ($i < $num){
$row = mysql_fetch_array($result);
if ( ($password) == $row['password'] && ($username) == $row['username'] ){
header('location:2.php');
$_SESSION['sessionname'] = $username;
$_SESSION['sessionpass'] = $password;
}
elseif ( ($password) != $row['password'] && ($username) == $row['username'] ) {
echo "Wrong Password <a href='1.php' >Click Here</a>";
}
$i++;
}
}else {
echo "Username <strong><u>$_POST[username]</u></strong> invalid ! <a href='1.php' >Click Here</a> ";
}
?>
没有理由按照您的方式分配用户名和密码变量。只需将post数据分配给变量,就像您通常使用会话一样。