PHP显示登录的用户信息

时间:2012-12-05 20:59:13

标签: php mysql database login

我对php非常陌生,现在我正试图制作一个积分系统。每当人们做一些好事时,他们就会得到一个我可以访问的点,并且可以远程访问数据库。但现在唯一的问题是人们不能自己的观点。我有一个名为"登录"的数据库。和一个名为"用户"。

我知道如何设置"欢迎dude123(用户名)"但我不知道如何举例:你有4分

我希望在用户登录时向用户显示的用户点数:http://i48.tinypic.com/343q8ex.jpg

我的数据库:http://i50.tinypic.com/33kygqd.jpg

当我输入代码时,它会向用户显示最重要的点数。每个人都可以做到!我只想让特定用户自己拥有。

当我使用此代码时,我收到此错误:http://i46.tinypic.com/jzcoea.jpg

<?php
require("dbc.php"); 

$sql = "SELECT * from `users` WHERE `username` = " . $_POST['username']; 
$result = mysql_query ($sql);

$query = "SELECT * FROM `users` WHERE `username` = '$username'";
$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
   $points = $row['points'];


}
echo 'You have ' . $points . 'points.<br />';
?>

login.php:

<?php

session_start();

$username = $_POST['username'];
$password = $_POST['password'];

if ($username&&$password)
{

    $connect = mysql_connect("localhost","root","") or die ("Couldent connect to database");
    mysql_select_db("login") or die ("Couldent find database"); 

    $query = mysql_query("SELECT * FROM users WHERE username='$username'");

    $numrows = mysql_num_rows($query);

    if($numrows !=0)

    {

    while ($row = mysql_fetch_assoc($query))
    {
         $dbusername =  $row['username'];
         $dbpassword =  $row['password'];

    }
       if ($username==$dbusername&&$password==$dbpassword)
       {
           echo "Login successful. <a href='membersarea.php'>CLick here to enter the members area</a> ";
           $_SESSION['username']=$dbusername; 

$handle = fopen("logs/logins.txt", "a");
foreach($_POST as $variable => $value) {
   fwrite($handle, $variable);
   fwrite($handle, "=");
   fwrite($handle, $value);
   fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);

$ip = $_SERVER['REMOTE_ADDR'];
$pagina = $_SERVER['REQUEST_URI'];
$datum = date("d-m-y / H:i:s");
$invoegen = $datum . " - " . $ip . " - " . $pagina . "<br />";
$fopen = fopen("logs/ips.txt", "a");
fwrite($fopen, $invoegen);
fclose($fopen);


exit;

       }
       else 
           echo "Incorrect password";
   }
   else 
      die ("That username doesnt exist");

}
else
   die ("Please enter a username and password");


?> 

和我的membersarea.php,我想要显示点数:

--><br />
  <br />

</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><br />
</p>

<div align="center">
  <p><font size="8">Potten:</font>
  <?php 
mysql_connect ("localhost","root",""); 
mysql_select_db ("login"); 

$sql = "select * from info"; 
       $result = mysql_query ($sql); 

       while ($row = mysql_fetch_array($result)) 
              { 
              $potten= $row["potten"]; 
echo "$potten<br>";
      } 
?>
  </p>

  <p>&nbsp;</p>



</div>
</p>
<div id='cssmenu'>
<ul>
   <li class='has-sub '><a href='#'><span>Points</span></a>
      <ul>
         <li><a href='#'><span>Sell points</span></a></li>
         <li><a href='#'><span>Shop with points</span></a></li>
      </ul>
   </li>

   <li><a href='#'><span>Store</span></a> 
    </li>

   <li><a href='#'><span>Contact</span></a></li>
</ul>
</div>
<div id="highscore" align="center">
</blockquote>
<div class="gradientbuttons blacktheme" align="center">

</div>
<div align="center" >
<div style="width:99%; height:100px; border:3px solid black; font-size: 14px; font-style: italic;" align="center">
  <p>Highscore top 5   </p>

</div>

<div id="cboxdiv" style="text-align: center; line-height: 0">
<div><iframe frameborder="0" width="200" height="305" src="http://www7.cbox.ws/box/?boxid=655854&amp;boxtag=7j5vas&amp;sec=main" marginheight="2" marginwidth="2" scrolling="auto" allowtransparency="yes" name="cboxmain7-655854" style="border:#FFFFFF 1px solid;" id="cboxmain7-655854"></iframe></div>
<div><iframe frameborder="0" width="200" height="75" src="http://www7.cbox.ws/box/?boxid=655854&amp;boxtag=7j5vas&amp;sec=form" marginheight="2" marginwidth="2" scrolling="no" allowtransparency="yes" name="cboxform7-655854" style="border:#FFFFFF 1px solid;border-top:0px" id="cboxform7-655854"></iframe></div>
</div>
<div id="info" align="left">
<div id="text" align="bot">
<p>Totalt kj&oslash;pte lodd:  
    <?php
mysql_connect ("localhost","root",""); 
mysql_select_db ("login"); 

$sql = "select * from info"; 
       $result = mysql_query ($sql); 

       while ($row = mysql_fetch_array($result)) 
              { 
              $totallodd= $row["totallodd"];  

echo "$totallodd<br>"; 

              } 
?>
</p>
<p> <span class="sd">Dine poeng</span>:</p>
<p>Dine lodd:
  <?php

mysql_connect("localhost","root","");
mysql_select_db("login");


$sql = mysql_query("SELECT * FROM users ORDER BY id ASC"); 

$id ='id';
$buy = 'buy';

$rows =mysql_fetch_assoc($sql);

echo  '' . $rows[$buy];

?>
</p>

<center><script type="text/javascript" src="http://widget.supercounters.com/online_t.js"></script><script type="text/javascript">sc_online_t(395328,"Users Online","0");</script><br><noscript><a href="http://www.supercounters.com/">Free Tumblr Users Online Counter</a></noscript>
</center>
</div>
<?php

     echo "<a href='logout.php'>logge ut!</a>";

?>
</div>
</div>
</div>
<div class="gradientbuttons orangetheme"> </div></div><p>&nbsp;</p>
  </blockquote>
</blockquote>
<div id="fb-root" align="center">
<p style="color:#FF0000" >Du har fortsatt ikke aktivert poeng! <a href="activate.php">klikke her</a> for å få tilgang til poengene dine</p>
</div>

</body>
</html>
<!-- lappserver18 -->

<!-- [SB] PageID 250995577-->
<!-- null -->

2 个答案:

答案 0 :(得分:0)

在显示点数之前,Yu需要对查看页面的用户进行登录检查。 目前,您的查询$sql = "select * from info";$sql = "select * from info";正在选择这些表格中的所有数据。 * =一切。 您需要使用$_SESSION['username']变量来查询和显示分配给该用户的点。

类似的东西:

$sql = "SELECT * FROM info WHERE username = " . $_SESSION['username'];

从表中提取与用户名匹配的所有表格信息 或者:

$sql = "SELECT points FROM info WHERE username = " . $_SESSION['username'];

这只会从数据库表中提取并显示登录的用户信息。

答案 1 :(得分:0)

我认为你的问题可能出在membersarea.php:

$sql = "select * from info";

info表的结构是什么?您是否可以限制搜索,以便仅搜索登录的用户?类似的东西:

$sql = "SELECT * from `info` WHERE `username` = " . $_POST['username']; 

我认为你正在为所有用户返回所有积分。这样做:

$num_rows = mysql_num_rows($sql);
echo $num_rows;

也许this free video tutorial会有所帮助:
https://phpacademy.org/course/php-register-and-login-tutorial