dskvfdxgdo2422392 2013-07-16 20:02
浏览 10

如何将用户注册详细信息检索到网页?

Hai i am new to the PHP programming.I was create register page and login page.when user login by using username and password,i want to display the user register detail.I want to know how to retrieve user register details into webpage.

I am using this code for reg and login.

For Register code:

 <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 :&nbsp;<br><br></td><td><input type="text" name="name" id="name">
 <br><br></td></tr>

 <tr><td>Password :&nbsp;<br><br></td><td><input type="password" name="pass" id="pass">
 <br><br></td></tr> 
 <tr><td>Email    :&nbsp;<br><br></td><td><input type="text" name="email" id="email">
 <br><br></td></tr> 
 <tr><td>Mobile No:&nbsp;<br><br></td><td><input type="text" name="mobile" id="mobile">
 <br><br></td></tr>

 <tr><td>Gender      :&nbsp;<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>

Register_db code:

 <?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;
} 
?>

For Login Code:

<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&nbsp;<a href="reg.php">Sign Up</a>  
 </b><br><br> 
 </form>
</div>
</body>
</html>

For Login_db Code:

<?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');
} 

?>

Here i am trying to display the user profile 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);
?>      

I am using this code to retrieve login user details,but unable to display details,it display username only.Please help me.

  • 写回答

1条回答 默认 最新

  • dongshen9058 2013-07-16 20:09
    关注

    You are missing the { } delimiters of your while loop.

    It should be like :

    echo "<table border='0'>";
    while($r = mysql_fetch_array($res))
    {
        // echo what you need
    }
    echo "</table>";
    

    Plus, the use of mysql_* functions is deprecated, you should consider using mysqli_* or PDO.

    评论

报告相同问题?