PHP Not'同步'用MySQL DB?

时间:2015-07-28 14:28:54

标签: php mysql mysqli phpmyadmin

我有一个包含用户信息的mySQL数据库。使用正常工作的用户注册系统将信息添加到数据库。这是一个示例用户(PHPmyadmin):enter image description here

问题是我有一个基于PHP的'我的帐户'检索用户帐户信息的页面。当我从PHPMyAdmin更新用户信息时,或者甚至在我添加关于用户的新数据时(例如,我的PHP未检测到新添加的&#),此信息似乎与MySQL不同步。 39; RealName'字段,虽然它识别已存在一段时间的“电子邮件地址”字段。

PHP / HTML:

<div class="modal-body">
        <img src="assets/user.jpg" style="border-radius:150px;height:150px;width:150px;float:left;"><br>
        <h2 class="desc" style="font-style: normal;padding:0px;margin:0px;float:left;">&nbsp <?=$_SESSION['RealName']?></h2><br><br>
        <h4 class="desc" style="font-style: normal;padding:0px;margin:0px;float:left;"><i>&nbsp &nbsp @<?=$_SESSION['Username']?></i></h4><br><br>
        <hr>
        <a href=""><p class="desc" style="font-style: normal;float:left;">&nbsp &nbsp Change Profile Image...</h2></a> <a href=""><p class="desc" style="font-style: normal;float:left;">&nbsp &nbsp Change Your Name...</h2></a>
        </div>
        <div class="modal-footer-alt"><br><br>
        <h4>Account Info &nbsp</h4>
        <p class="desc" style="font-style: normal;float:left;"><b>E-Mail Address: </b><?=$_SESSION['EmailAddress']?> &nbsp</p> <code style="float:left;">Private</code><br><br>
        <p class="desc" style="font-style: normal;float:left;"><b>Password: </b> ●●●●●●●●●● &nbsp</p> <code style="float:left;">Private</code><br>
        <h4>Bio &nbsp</h4>
        <textarea rows="4" cols="40"><?=$_SESSION['Bio']?></textarea>
        <h4>More &nbsp</h4>
        <a href="../account/die.php"><button class="btn btn-default">Log Out</button></a><a href="../account/die.php"><button class="btn btn-default">Sign Up for Contributor Rewards</button></a>
</div>

它在网页上的显示方式: enter image description here 请注意,某些信息会显示,而其他信息则不会显示。这不是我的HTML的问题,好像我用静态值替换PHP,它显示正常。

这是我用来启动会话的代码:

<?php
session_start();

$dbhost = "PRIVATE"; // this will ususally be 'localhost', but can sometimes differ
$dbname = "PRIVATE"; // the name of the database that you are going to use for this project
$dbuser = "PRIVATE"; // the username that you created, or were given, to access your database
$dbpass = "PRIVATE"; // the password that you created, or were given, to access your database

mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>

注销代码:

<?php include "base.php"; $_SESSION = array(); session_destroy(); ?>

2 个答案:

答案 0 :(得分:3)

我不同意您的工作流程,不会使用此方法。在与用户相关的所有内容中,我只信任来自数据库(或验证授权机构,例如OAuth或OpenId)的最新数据,从不存储会话数据。

每当我需要像userdata这样的关键信息时,我会查询数据库。用户可以在会话中间更改他/她的名字,这应该由您的系统实时反映。

我也会避免OR DIE。请考虑使用try...catch并处理错误。

try {
  include ('ConnectionData.php'); // user, pass, db, localhost
  $sqlGetId = "SELECT etcetcetc";
  $sqlResultGetId = mysql_query($sqlGetId);
  $rowGetId = mysql_fetch_array($sqlResultGetId);
  if ( $rowGetId[0] > 0 ) {
    // consume data

答案 1 :(得分:2)

您需要重新加载$_SESSION个值...这可以通过创建页面logout.php并在其中包含函数session_start(); session_destroy();来实现。您的登录页面需要更新才能获得正确的会话数据。