简体   繁体   中英

How to echo data from mysql current session in php?

Before I start I must say I'm a huge noob to php. I'm following a tutorial online on how to make a login/registration php page with MySQL: https://daveismyname.blog/login-and-registration-system-with-php

I added my own SQL column named 'investment' as int(11).

I am able to echo SESSION username, but not session investment.

The default value of the investment column is '0' not sure if this matters, but stating it anyway.

When the echo is called the spot where the amount is supposed to be is just empty.

index.php(echo of investment):

<a href="#" class="dropdown-toggle waves-effect waves-button waves-classic fa fa-usd" data-toggle="dropdown"> Invested Amount: $
  <?php echo $_SESSION['investment'];?>
  <i class="fa fa-angle-down">
  </i>
</a>
<ul class="dropdown-menu title-caret dropdown-lg" role="menu">
  <li class="drop-all">
    <a href="#" class="text-center">Portfolio
    </a>
  </li>
  <li class="drop-all">
    <a href="#" class="text-center">Invest
    </a>
  </li>
</ul>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle waves-effect waves-button waves-classic" data-toggle="dropdown">
    <span class="user-name">
      <?php echo htmlspecialchars($_SESSION['username'], ENT_QUOTES); ?>
      <i class="fa fa-angle-down">
      </i>
    </span>
  </a>

Code handling SESSION:

<?php
include('password.php');
class User extends Password{
    private $_db;
    function __construct($db){
        parent::__construct();
        $this->_db = $db;
    }
    private function get_user_hash($username){
        try {
            $stmt = $this->_db->prepare('SELECT password, username, memberID, investment,email FROM members WHERE username = :username AND active="Yes" ');
            $stmt->execute(array('username' => $username));
            return $stmt->fetch();
        } catch(PDOException $e) {
            echo '<p class="bg-danger">'.$e->getMessage().'</p>';
        }
    }
    public function isValidUsername($username){
        if (strlen($username) < 3) return false;
        if (strlen($username) > 17) return false;
        if (!ctype_alnum($username)) return false;
        return true;
    }
    public function login($username,$password){
        if (!$this->isValidUsername($username)) return false;
        if (strlen($password) < 3) return false;
        $row = $this->get_user_hash($username);
        if($this->password_verify($password,$row['password']) == 1){
            $_SESSION['loggedin'] = true;
            $_SESSION['username'] = $row['username'];
            $_SESSION['memberID'] = $row['memberID'];
            $_SESSION['email'] = $row['email'];
            $_SESSION['investment'] = $row['investment'];
            return true;
        }
    }
    public function logout(){
        session_destroy();
    }
    public function is_logged_in(){
        if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
            return true;
        }
    }
}
?>

here's an example. Call your current ID session then select in database. you dont have to session all your data.

$crud = new crud($DB_con);

$id = $_SESSION['id'];

$stmt = $crud->runQuery("SELECT * FROM tbl_login WHERE id=:id");
$stmt->execute(array(":id"=>$id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
echo $userRow['investment'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM