如何在用户名登录后显示用户名?

时间:2014-11-26 16:54:42

标签: php html

抱歉,如果这是重复问题,我确实试图在互联网上找到答案并使用它..但我仍然可以完成...这里我的问题...我想知道登录时如何显示名称没用id?它就像用户登录后使用他们的无ID一样欢迎屏幕。在这里我的代码和对不起,如果它搞砸了... 我的login.php

<?php


$host="localhost"; // Host name
$username="root"; // Mysql username
$password="1234"; // Mysql password
$db_name="vronline"; // Database name
$tbl_name="user_information"; // Table name

// Connect to server and select databse.
$con = 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'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);

$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);


$sql="SELECT * FROM $tbl_name WHERE user_id='$myusername' and user_password='$mypassword'" ;
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");

 $row=mysql_fetch_array($result);
                        if ($row['group_id']==0){
                        header("location:../user.php");
                        }
                        elseif ($row['group_id']==1) {
                        header("location:../admin.php");
                        }
//header("location:../menu.php");
}


else {
echo "Wrong Username or Password";
}
?>

这是我的user.php

<?php
session_start();
if(!session_is_registered(myusername)){
header("location:../login_main.php");
}

?>

<?php
include ('php/dbconnect.php');

$matric = $_session['myusername'];

mysql_query("SELECT name FROM user_information WHERE user_id=".$_SESSION['myusername']);


?>
<html>
<head>
<title>user</title>
<link href="source/loginstyle.css" rel="stylesheet" type="text/css" media="all" />
<link href="source/kepala.css" rel="stylesheet" type="text/css" media="all" />
<link href="source/content.css" rel="stylesheet" type="text/css" media="all" />
<link href="source/menuBox.css" rel="stylesheet" type="text/css" media="all" />
<link href="source/bottomfoot.css" rel="stylesheet" type="text/css" media="all" />
<link href="source/accessibility_foundicons.css" rel="stylesheet" type="text/css" media="all"/>
<link href="source/general_foundicons.css" rel="stylesheet" type="text/css" media="all" />

<script src="source/Chart.js"></script>
</head>
<body>

<div>
<!--head-->
<div class= "kepala">


<h1>
<a href="#"><span class="foundicon-smiley"></span></a> <?php $row['name'];  ?>
</h1>


</div>


<div class= "content">


<h4>content</h4>

<div class="menu">
<div class="menu_box_list">
                <ul>
                    <li><a href="php/displayuseronly.php"><span class="foundicon1-person"></span> manage user<i class="manageuser"> </i><div class="clear"></div> </a></li>
                    <li><a href="userAddvehicle.php"><span> </span>vehicle manager <i class="vehicle manager"> </i><div class="clear"></div> </a></li>
                    <li><a href="#"><span class= "foundicon-page"></span> report <i class="Report"> </i><div class="clear"></div> </a></li>
                    <li><a href="#"><span class= "foundicon-settings"></span> log out<i class="settings"> </i><div class="clear"></div> </a></li>
                </ul>
                <div class="clear"></div>
            </div>


</div>
<div class = "2nd_menu">

</div>


</div>

<div class= "Bfooter">
<h4></h4>



</div>

</div>

</body>
</head>
</html

我无法解决这段代码

  

mysql_query(“SELECT name FROM user_information WHERE user_id =”。$ _ SESSION ['myusername']);

如何让它显示名称而不是user_id? 对不起,我对此并不是很好 对不起,我忘了添加这个 我想在这里显示用户名

  

<a href="#"<span></span></a> <?php $row['name']; ?>

每当我写的时候我就不知道那里的名字..它要么不显示任何内容或错误

2 个答案:

答案 0 :(得分:0)

在您的login.php页面中使用此代码

header("location:../user.php");
session_start();
$_SESSION['myusername']=$myusername;

在user.php

中试试
ob_start("ob_gzhandler");
session_start();
$matric = $_session['myusername'];

$result=mysql_query("SELECT name FROM user_information WHERE user_id='$matric'");
if(mysql_num_rows($result)>0)
{
while($r=mysql_fetch_array($result))
{
    $name=$r['name'];
   echo $name;
}
}

答案 1 :(得分:0)

你可能想试试这个:

$qry_sel = "SELECT name FROM user_information WHERE user_id=".$_SESSION['myusername'].";";
$qry_res = mysql_query($qry_sel);
$row = mysql_fetch_array($qry_res);

echo $row['name'];