试写了一个登陆界面,还没有完成,测试的时候发现问题。$_POST里的值不会清空,每输入一次用户名,$_POST里面就多一个用户名,后果就是第一次错误输入的话,就无法再正确输入用户名除非浏览器刷新页面<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Welcome back</title>
<style type="text/css">
.auto-style2 {
text-align: center;
}
.auto-style1 {
font-size: 36pt;
text-align: left;
}
.auto-style4 {
font-family: Broadway;
font-size: xx-large;
}
</style>
</head><body><table align="center" style="width: 100%">
<tr>
<td>
<div class="auto-style2">
<table align="center" style="width: 100%">
<tr>
<td class="auto-style1">Welcome back</td>
<td>
<img height="89" src="logo.jpg" style="float: right" width="184" /></td>
</tr>
</table>
</div>
<hr />
<p class="auto-style4">Log in:</p>
<form action="login.php" method="post" style="height: 213px">
<div class="auto-style2">
<table style="width: 100%">
<tr>
<td class="auto-style2">User Name:</td>
</tr>
<tr>
<td class="auto-style2">
<input name="user_name" type="text" value="
<?php 
if(isset($_POST['user_name']))
{
print htmlspecialchars($_POST['user_name']);
}
?>
"/>
</td>
</tr>
<tr>
<td class="auto-style2">Password:</td>
</tr>
<tr>
<td class="auto-style2">
<input name="password" type="password" /></td>
</tr>
</table>
<br />
<br />
<input name="submit" type="submit" value="Sign in" /></div>
</form>
</td>
</tr>
</table><?phprequire_once 'connectDB.php';print '<style type = "text/css" media = "screen">.error {color: red;}</style>';if($_SERVER['REQUEST_METHOD']=='POST')
{
$problem = false;
$login = false; if(empty($_POST['user_name']))
{
$problem = true;
print '<p class = "error">Please enter your user name!</p>';
}
if(empty($_POST['password']))
{
$problem = true;
print '<p class = "error">Please enter a password</p>';
} if(!$problem)
{
$user = trim(stripslashes($_POST['user_name']));
$pwd = trim(stripslashes($_POST['password']));
$user = mysql_real_escape_string($user);
$pwd = mysql_real_escape_string($pwd);

$q = "SELECT * FROM USER WHERE USER_NAME = '$user' and PASSWORD = '$pwd'";

echo $q;

$result = mysql_query($q);

if(!$result || mysql_num_rows($result) == 0)
{
print '<p class = "error">Wrong Username or Password.</p>';
}
else
{
$row = mysql_fetch_array($result);
$id = $row['USER_ID'];
$name = $row['USER_NAME'];
echo "User ID is ".$id;

session_start();
$_SESSION['$id'] = $id;

//clear the POST array
$_POST = array();   //网上找来的办法, 但貌似不管用

}
}
else
{
print '<p class = "error">Please try again!</p>';
}
}?></body></html>
请高手帮忙解决一下,感激不尽MySQLHTML$_POST

解决方案 »

  1.   

                                <input name="user_name" type="text" value="
                                    <?php 
                                        if(isset($_POST['user_name']))
                                        {
                                            print htmlspecialchars($_POST['user_name']);
                                        }
                                    ?>
                                "/>
    改为                            <input name="user_name" type="text" value="<?php 
                                        if(isset($_POST['user_name']))
                                        {
                                            print htmlspecialchars($_POST['user_name']);
                                        }
                                    ?>"/>
    即可
    是 value 中的空格和回车换行符在作怪
      

  2.   

    谢谢版主大神。小弟刚刚学习php和网页制作,大神是否能解释一下,空格和回车符作怪的原因吗?
      

  3.   

    input type=text 是单行文本编辑器
    如果你给他的是多行的文本,那么只有第一行可以被编辑。这就是你遇到的问题的原因
      

  4.   

    本帖最后由 xuzuning 于 2013-04-22 10:14:12 编辑
      

  5.   

    可能是 你post 赋值时有问题,post 的值只会覆盖上一个,怎么会附加呢..