如何从数据库中获取用户名,并在用户登录时将其显示给用户?

时间:2017-02-12 11:13:57

标签: javascript php jquery mysqli

这是我的登录页面。我从这里得到什么代码或查询,并添加到我想显示用户名的位置,以显示用户的全名?

<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'university portal');
define('DB_USER','root');
define('DB_PASSWORD','password007');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
/*
$ID = $_POST['user'];
$Password = $_POST['pass'];
*/
function SignIn()
{
session_start();   //starting the session for user profile page
if(!empty($_POST['UserName']))   //checking the 'user' name which is from Sign-In.html, is it empty or have some text
{
    $query = mysql_query("SELECT Username, Password, voting_status FROM voters where Username = '$_POST[UserName]' AND Password = '$_POST[password]'") or die(mysql_error());
    $row = mysql_fetch_array($query) or die(mysql_error());
    if(!empty($row['Username']) AND !empty($row['Password']))
    {

        $_SESSION['Username'] = $row['Password'];
        $_SESSION['Voting Status'] = $row['voting_status'];
        header("location:Voters Account.php");

    }
    else{
        echo "username or password is incorrect";
    }
}
}
if(isset($_POST['submit']))
{
    SignIn();
}
          ?>

这是我想要显示用户全名的地方,所以请真的需要帮助,谢谢。

<!doctype html>
<html>
<head>
<link href="css/Layout.css" rel="stylesheet" type="text/css" />
<link href="css/Menu.css" rel="stylesheet" type="text/css" />
<meta charset="utf-8">
<title>Student Welcome</title>

<link rel="stylesheet" href="themes/bar/bar.css" type="text/css" media= "screen" /> 
    <link rel="stylesheet" href="nivo-slider.css" type= "text/css" media="screen" />
    <script src="jquery.min.js" ></script> 
    <script type="text/javascript" src="jquery.nivo.slider.js" ></script>
</head>

<body>
<div id="Holder">
    <div id="Header">
        <a href="Home.php"><img src="assets/Logo.png"></a>
    </div>
    <div id="NavBar">
        <nav>
        <ul>
            <li><a href="Home.php">Home</a></li>
            <li><a href="Login.php">Student Portal</a>
            <ul>
                <li><a href="Login.php">Login</a></li>
                <li><a href="Candidate Profile.php">Candidates Profile</a></li>
            </ul>
            </li>
            <li><a href="Admin.php">Admin</a></li>
            <li><a href="About Site.php">About Us</a>
            <ul>
                <li><a href="About Site.php">About Site</a></li>
                <li><a href="Contact Us.php">Contact Us</a></li>
            </ul>
            </li>
        </ul>
    </nav>
    </div>
    <div id="Content">
        <div id="PageHeading">
      <h1>&nbsp;</h1>
      <h1>&nbsp;</h1>
      <h1>&nbsp;</h1>
      <h1>Welcome, <?php echo $row_Voters_Account_Form['Fname']; ?> <?php echo $row_Voters_Account_Form['Mname']; ?> <?php echo $row_Voters_Account_Form['Lname']; ?>!</h1>
      </div>
    <div id="ContentLeft">
      <h2>Your Message Here</h2></b>
      <h6>This site is to improve the style of voting in West End University College and other school at large, if it is implementted. </h6>
      <h6>&nbsp;</h6>
      <h6>Note: Please be advice that once you have placed your  vote, you won't be allowed to login again. so there for you can only view the result from of the election from the Hoe page.</h6>
    </div>
    <div id="ContentRight">
      <form id="form1" name="form1" method="post">
      </form>
      <table width="400" border="0" align="center">
        <tbody>
          <tr>
            <td>
                <div id="slider" class="nivoSlider">
            <img src="slider/IMG-20151117-WA00011234.jpg" name="imageField" data-thumb= "slider/IMG-20151117-WA00011234.jpg" alt="" title="These are the candidates for the election"/>
            <img src="slider/FB_IMG_1484460719710123.jpg" data-thumb="slider/FB_IMG_1484460719710123.jpg" alt ="" title="These are the candidates for the election" />
            <img src="slider/FB_IMG_14845217848411234567.jpg" data-thumb= "slider/FB_IMG_14845217848411234567.jpg" alt="" title="These are the candidates for the election"/>
            <img src="slider/IMAG002012345678.jpg" data-thumb= "slider/IMAG002012345678.jpg" alt="" title="These are the candidates for the election"/>
            <img src="slider/FB_IMG_1484461298331123456.jpg" data-thumb= "slider/FB_IMG_1484461298331123456.jpg" alt="" data-transition="slideInRight" title="These are the candidates for the election"/>
            <img src="slider/FB_IMG_148446110711012345.jpg" data-thumb="slider/FB_IMG_148446110711012345.jpg" alt="" title="#htmlcaption" />
        </div>

        <script type="text/javascript">
     $(window).load(function() { 
     $('#slider').nivoSlider({ 
     effect:'random', 
     slices: 15, 
     boxCols: 8, 
     boxRows: 4, 
     animSpeed: 500, 
     pauseTime: 3000,
     startSlide: 0, 
     directionNav:true, 
     controlNav:true, 
     controlNavThumbs:false, 
     pauseOnHover:true, 
     manualAdvance:false, 
     prevText:'Prev', 
     nextText:'Next', 
     randomStart:false, 
     beforeChange:function(){}, 
     afterChange:function(){}, 
     slideshowEnd:function(){}, 
     lastSlide:function(){}, 
     afterLoad:function(){} 
     }); 
     }); 
    </script>
            </td>
          </tr>
        <table width="0" border="0" align="center">
          <tr>
            <td>
                <input type="button" name="button" id="button" value="Click here to cast your vote" onClick="window.location.href='Voting.php' ">
            </td>
          </tr>
        </table>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
        </tbody>
      </table>
    </div>
    </div>
    <div id="Footer"></div>
</div>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Voters_Account_Form);
?>

2 个答案:

答案 0 :(得分:0)

在[用户名]行中打印用户名:

echo $row['Username'];

答案 1 :(得分:0)

首先登录代码更改

 $_SESSION['Username'] = $row['Password'];

 $_SESSION['Username'] = $row['username'];

如果要显示用户名的页面是登录后的页面,只需启动会话并显示如下用户名

echo $_SESSION['Username'];

希望有所帮助