海我是 PHP 编程的新手。我正在创建注册页面和登录页面。当用户使用用户名和密码登录时,我想显示用户注册详细信息。我想知道如何将用户注册详细信息检索到网页中。
我正在使用此代码进行注册和登录。
对于注册码:
<html>
<title>Register Page</title>
<script type="text/javascript">
function validateForm()
{
var a=document.f.name.value;
var b=document.f.pass.value;
var c=document.f.email.value;
var d=document.f.mobile.value;
var e=document.f.sex.value;
if ((a==null || a=="") && (b==null || b=="") && (c==null || c=="") &&
(d==null || d==""))
{
alert("All Field must be filled out");
return false;
}
if (a==null || a=="")
{
alert("Username must be filled out");
return false;
}
if (b==null || b=="")
{
alert("password must be filled out");
return false;
}
if (c==null || c=="")
{
alert("Email must be filled out");
return false;
}
if (d==null || d=="")
{
alert("Enter the mobile number");
return false;
}
if(e=="Select")
{
alert("Please choose gender");
return false;
}
}
</script>
<style>
#abc
{
background-color:#FF3366;
}
</style>
<body bgcolor="#0099CC">
<h1 id="abc"><center>Welcome to the register page</center></h1>
<form name="f" action="regdb.php" method="post" onSubmit="return validateForm()">
<table align="center">
<tr><td>Fullname : <br><br></td><td><input type="text" name="name" id="name">
<br><br></td></tr>
<tr><td>Password : <br><br></td><td><input type="password" name="pass" id="pass">
<br><br></td></tr>
<tr><td>Email : <br><br></td><td><input type="text" name="email" id="email">
<br><br></td></tr>
<tr><td>Mobile No: <br><br></td><td><input type="text" name="mobile" id="mobile">
<br><br></td></tr>
<tr><td>Gender : <br><br></td><td><select name="sex"><option>Select</option>
<option>Male</option>
<option>Female</option></select><br><br></td></tr>
<tr><td></td><td><input type="submit"
value="register"></td></tr>
</table>
</form>
</body>
</html>
注册数据库代码:
<?php
$con=mysql_connect("localhost","root","");
if(!$con)
{
die("could not connect:".mysql_error());
}
mysql_select_db("manoj",$con);
if (isset($_POST['name']) && isset($_POST['pass']) && isset($_POST['email']) &&
isset($_POST['mobile']) && isset($_POST['sex']))
{
//Prevent SQL injections
$username = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$mobile = mysql_real_escape_string($_POST['mobile']);
$sex = mysql_real_escape_string($_POST['sex']);
//Get MD5 hash of password
$password = $_POST['pass'];
//Check to see if username exists
$sql = mysql_query("SELECT name FROM register WHERE name = '".$username."'");
if (mysql_num_rows($sql)>0)
{
echo "The username you have entered is already exist. Please try another username.";
echo '<a href="reg.php">Try Again</a>';
exit;
}
mysql_query("INSERT INTO register (name, password, email,mobile,sex) VALUES (
'$username', '$password', '$email','$mobile','$sex')");
echo "Thank You for Registration.";
echo '<a href="login.php">Click Here</a> to login you account.';
exit;
}
?>
对于登录代码:
<html>
<head>
<title>Php Simple Login Form</title>
</head>
<script type="text/javascript">
function validateForm()
{
var a=document.f.username.value;
var b=document.f.password.value;
if ((a==null || a=="") && (b==null || b==""))
{
alert("All Field must be filled out");
return false;
}
if (a==null || a=="")
{
alert("Username must be filled out");
return false;
}
if (b==null || b=="")
{
alert("password must be filled out");
return false;
}
}
</script>
<style>
#abc
{
background-color: #EFFBEF;
text-decoration: blink;
}
#def
{
background-color:#00BFFF;
}
</style>
<body bgcolor="#00BFFF">
<div id="containt" align="center">
<form name="f" action="logindb.php" method="post" onSubmit="return validateForm()">
<center><h1 id="abc">Welcome To My Page </h1></center>
<div id="header"><h2 class="sansserif"> Login Here</h2></div>
<table>
<tr>
<td>Enter Username:</td>
<td> <input type="text" name="username" size="20"></td>
</tr>
<tr>
<td>Enter Password:</td>
<td><input type="password" name="password" size="20"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Log In"></td>
</tr>
</table>
<b>Don't have an account,please click here <a href="reg.php">Sign Up</a>
</b><br><br>
</form>
</div>
</body>
</html>
对于 Login_db 代码:
<?php
session_start();
$con=mysql_connect("localhost","root","");
if(!$con)
{
die("could not connect:".mysql_error());
}
mysql_select_db("manoj",$con);
$username = $_POST["username"];
$password = $_POST["password"];
$match = "select id from register where name = '".$_POST['username']."' and password =
'".$_POST['password']."'";
$qry = mysql_query($match);
$num_rows = mysql_num_rows($qry);
if ($num_rows<=0 )
{
echo "Incorrect username/password";
header('Location: login.php');
}
else
{
//have them logged in
$_SESSION['user']= $_POST["username"];
header('Location: profile.php');
}
?>
在这里,我试图显示用户配置文件 details-profile.php:
<?php
session_start();
echo "You are Welcome ". $_SESSION['user'];
$con=mysql_connect("localhost","root","");
if(!$con)
{
die("could not connect:".mysql_error());
}
mysql_select_db("manoj",$con);
$sql="select * from register where name = '".$_SESSION['user']."'";
$res=mysql_query($sql);
echo "<table border='0'>";
while($r=mysql_fetch_array($res))
echo"<tr>"."<td>"."Name :"."</td>"."<td>".$r['name']."</td>"."</tr>";
echo"<tr>"."<td>"."Password :"."</td>"."<td>".$r['password']."</td>"."</tr>";
echo"<tr>"."<td>"."Email :"."</td>"."<td>".$r['email']."</td>"."</tr>";
echo"<tr>"."<td>"."Mobile Number :"."</td>"."<td>".$r['mobile']."</td>"."</tr>";
echo"<tr>"."<td>"."Gender :"."</td>"."<td>".$r['sex']."</td>"."</tr>";
echo "</table>";
echo"<p align='right'><a href='logout.php'>Logout</a></p>";
mysql_close($con);
?>
我正在使用此代码检索登录用户详细信息,但无法显示详细信息,它仅显示用户名。请帮助我。