gpt4 book ai didi

php - 如何在PHP中从MySQL当前 session 回显数据?

转载 作者:行者123 更新时间:2023-11-30 23:44:31 24 4
gpt4 key购买 nike

在我开始之前,我必须说我是php的新手。我正在在线学习有关如何使用MySQL进行登录/注册php页面的教程:https://daveismyname.blog/login-and-registration-system-with-php

我将自己的名为“投资”的SQL列添加为int(11)。

我能够回应SESSION用户名,但不能回应会话投资。

投资列的默认值为“ 0”,不确定是否重要,但无论如何都要声明。

当将回声称为应有的光斑时,它只是空的。

index.php(投资回声):

<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>


代码处理环节:

<?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;
}
}
}
?>

最佳答案

这是一个例子。调用您当前的ID会话,然后在数据库中选择。您不必会话所有数据。

$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'];

关于php - 如何在PHP中从MySQL当前 session 回显数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48335424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com