如何在php中创建用户欢迎消息。这样,已登录的用户将能够看到他的用户名。 我有这个代码,但似乎没有用。
<?php
$con = mysql_connect("localhost","root","nitoryolai123$%^");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("school", $con);
$result = mysql_query("SELECT * FROM users
WHERE Username='$username'");
while($row = mysql_fetch_array($result))
{
echo $row['Username'];
echo "<br />";
}
?>
我正在尝试使用此登录表单中输入的数据:
<form name="form1" method="post" action="verifylogin.php">
<td>
<table border="0" cellpadding="3" cellspacing="1" bgcolor="">
<tr>
<td colspan="16" height="25" style="background:#5C915C; color:white; border:white 1px solid; text-align: left"><strong><font size="2">Login User</strong></td>
</tr>
<tr>
<td width="30" height="35"><font size="2">Username:</td>
<td width="30"><input name="myusername" type="text" id="idnum" maxlength="5"></td>
</tr>
<tr>
<td width="30" height="35" ><font size="2">Password:</td>
<td width="30"><input name="mypassword" type="password" id="lname" maxlength="15"></td>
</tr>
<td align="right" width="30"><td align="right" width="30"><input type="submit" name="Submit" value="Submit" /></td> <td align="right" width="30"><input type="reset" name="Reset" value="Reset"></td></td>
</tr>
</form>
但是,这个,verifylogin.php,似乎在阻碍。
<?php
$host="localhost";
$username="root";
$password="nitoryolai123$%^";
$db_name="school";
$tbl_name="users";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
session_register("myusername");
session_register("mypassword");
header("location:userpage.php");
}
else {
echo "Wrong Username or Password";
}
?>
我该怎么办?运行时我总是遇到这个错误:
Notice: Undefined variable: username in C:\wamp\www\exp\userpage.php on line 53
您能否更轻松地了解如何实现同样的目标?
答案 0 :(得分:3)
$result = mysql_query("SELECT * FROM users WHERE Username='$username'");
你忘了从某个地方定义并填写$ username
答案 1 :(得分:0)
您还需要添加一个exit();在以下代码之后:
session_register("myusername");
session_register("mypassword");
header("location:userpage.php");
exit(); ## EXIT REQUIRED
如果您不添加此内容并且客户端刷新,系统会提示他们提示“您确定要重新提交已发布的变量吗?”如果他们单击是,那么所有登录逻辑将再次执行。在这种情况下,这可能不是致命的事情,但无论如何你应该直到拥有它。