我有一个包含Name
,Username
和Password
的登录表。当用户使用用户名和密码登录时,我想显示用户名而不是用户名。我想在下一页中以欢迎名称而不是欢迎USERNAME 显示用户名。
这是HTML:
<form class="form-basic" method="post" action="login.php">
<div class="form-row">
<label>
<span>Username</span>
<input type="text" name="username" id="username" >
</label>
</div>
<div class="form-row">
<label>
<span>Password</span>
<input type="password" name="password" id="password" >
</label>
</div>
<div class="form-row">
<button type="submit" name="submit" id="submit"/>SUBMIT</button>
</div>
</form>
PHP代码:
<?php
// your values are stored in cookies, then you can login without validate
include_once 'db.php';
// login validation in php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$username = mysql_real_escape_string(stripslashes($_POST['username']));
$password = $_POST['password'];
$query = mysql_query("select name,username,password from reglogin where username='".$username."' AND password='".$password."'");
$numrows = mysql_num_rows($query);
if ($numrows != 0) {
while ($row=mysql_fetch_assoc($query)) {
$dbname=$row['username'];
$dbpassword=$row['password'];
}
if($username == $dbname && $password == $dbpassword)
{
$_SESSION['sess_user']=$name;
echo "<script>window.open('registration1.php','_self')</script>";
}
}
else {
echo "<script>alert('Invalid! Username or Password')</script>";
}
}
?>
答案 0 :(得分:2)
你有输入
<input type="text" name="username" id="username" >
在php端,您正在访问
$name=$_POST['name'];
你必须将上面一行更改为
$name=$_POST['username'];
或者你必须更新
$_SESSION['sess_user']=$name;
到
$_SESSION['sess_user']=$row['username'];
<强>更新强>
因为您需要名称,所以您可以将名称设置为会话
$_SESSION['sess_user']=$row['name'];
答案 1 :(得分:1)
您可以使用以下代码替换您的代码: 您的错误:
查看您的查询您选择的内容和获取的内容。 我所做的更改:
希望这会对你有所帮助。
// your values are stored in cookies, then you can login without validate
include_once 'db.php';
// login validation in php
if (isset($_POST['submit'])) {
// $name = $_POST['name']; // this is not posted
$username = mysql_real_escape_string(stripslashes($_POST['username']));
$password = $_POST['password'];
$query = mysql_query("select name,username,password from reglogin where username='".$username."' AND password='".$password."'");
$numrows = mysql_num_rows($query);
if ($numrows != 0) {
while ($row=mysql_fetch_assoc($query)) {
$dbname=$row['username'];
$dbpassword=$row['password'];
$name =$row['name'];
}
if($username == $dbname && $password == $dbpassword)
{
$_SESSION['sess_user']=$name;
echo "<script>window.open('registration1.php','_self')</script>";
}
}
else {
echo "<script>alert('Invalid! Username or Password')</script>";
}
}
?>