到目前为止,我已经尝试了所有内容,例如选择$_SESSION['username']
和$_SESSION['user_name']
,我甚至试图用mysqli_*
说。顶部有session_start();
!
我正在尝试在我的SQL数据库中显示与用户会话ID或user_name
列对应的user_id
行。 This image here是我要在其上显示用户名的帐户页面。
的index.php
<div class="container">
<div style="font-size: 18px; font-weight: bold;">
<p>Account Info:</p>
</div>
<hr color="white">
<div class="accinfo">
<p style='display: inline;'>Username:
<?php
$_SESSION['user_name'] = "xxx";
$conn = mysqli_connect("localhost", "root", "", "kurticlan_forum");
$sql = mysqli_query($conn, "SELECT * FROM users WHERE user_name='user_name'");
$result = mysqli_num_rows($sql);
$username = $_SESSION['user_name'];
echo $_SESSION['user_name']
?>
</p>
<p>User Number: <?php echo $_SESSION['id'] ?></p>
<p>Password: *** |
<a style="text-decoration: none; color: blue; font-size: 12px" href="/account/changepassword.php">Change Password</a>
</p>
<br />
<p style="display: inline;">Account Picture:</p><img
</div>
</div>
MySQL数据库 View Image
在有人说'#34;这是一个重复的帖子&#34;因为我知道至少有人会...这是我的所有代码,我知道有很多帖子就是这样但这些帖子中的解决方案都没有帮助我...我已经尝试了没有第一个的代码$ _SESSION [&#39; user_name&#39;] =&#34; xxx&#34 ;; 它只是说变量&#34; user_name&#34; 是未定义的。我不知道发生了什么或为什么它不起作用!我也试过整个$ row的东西,但它说它也是未定义的。
答案 0 :(得分:0)
首先,您需要在标题上启动会话
session_start();
因为您已定义会话user_name的默认值
$_SESSION['user_name'] = "xxx";
您的查询将是这样的。
$sql = mysqli_query($conn, "SELECT * FROM users WHERE user_name='".$_SESSION['user_name']."'");
答案 1 :(得分:0)
重新编写代码。将其替换为旧代码并提供更新。
<div class="container">
<div style="font-size: 18px; font-weight: bold;">
<p>Account Info:</p>
</div>
<hr color="white">
<div class="accinfo">
<p style='display: inline;'>Username:
<?php
$sess_username= $_SESSION['user_name'];
$servername = "localhost";
$username = "root";
$password = "kurticlan_forum";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT user_name, user_id FROM users WHERE user_name= $sess_username";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$username = $row["user_name"];
$id = $row["user_id"];
}
} else {
echo "0 results";
}
mysqli_close($conn);
if($username == $sess_username)
{
echo $username;
?>
</p>
<p>User Number: <?php echo $id; ?></p>
<p>Password: *** |
<a style="text-decoration: none; color: blue; font-size: 12px" href="/account/changepassword.php">Change Password</a>
</p>
<br />
<p style="display: inline;">Account Picture:</p><img
</div>
</div>
<?php
}
?>
但您不是您需要的确切解决方案了解您正在做的事情。总是做一个功能来检查islogin !! 并称它为第1行你的PHP 该函数必须使用会话匹配检查用户名帖子。
然后你可以继续正常的代码。我只是在你给出的工作状态中写了你的代码..并且请使用准备好的陈述。
答案 2 :(得分:0)
我的问题的问题在于我的登录php脚本:
<?php
session_start();
include 'dbh.php'; //just connects to database
$user_name = $_POST['username'];
$user_pass = $_POST['password'];
$msg = "Your username or password is incorrect!";
$salt = "aJd32".$user_pass."iki9P";
$hash_pwd = hash('sha512', $salt);
$sql = "SELECT * FROM users WHERE user_name='$user_name' AND user_pass='$hash_pwd'";
$result = mysqli_query($conn, $sql);
if (!$row = mysqli_fetch_assoc($result)) {
echo "<script type='text/javascript'>alert('$msg');</script>";
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
$_SESSION['id'] = $row['user_id']; //I saw this row!
//I added rows here
header("Location: /account/");
}
?>
然后添加了这些行!
$_SESSION['user_name'] = $row['user_name'];
$_SESSION['user_email'] = $row['user_email'];
$_SESSION['user_date'] = $row['user_date'];
然后在我的index.php中我做了echo $_SESSION['user_name'];
并且它有效!我现在有一个工作脚本,它显示sql数据库中登录的用户名!