我在这样的php
页面上将用户名显示为链接,该页面在单击时会浏览到特定用户的主页:
$msql=$db->prepare("SELECT * from users where id=? order by id desc");
$msql->bind_param("i",$user_id);
$msql->execute();
$msql = $msql->get_result();
$msql = $msql->num_rows;
while($usercount=$msql->fetch_assoc())
{
$Email = $usercount['email'];
$FirstName = $usercount['first_name'];
$LastName = $usercount['last_name'];
?>
<strong><a href="?navigate=true" class="post_user" id="post_user" data-emailID="<?php echo $Email; ?>"><?php echo $FirstName.' '.$LastName;?></a></strong>
<?php
}
?>
为了在单击链接时导航到用户主页,我使用的是这样的:
<?php
if(isset($_GET['navigate']) && $_GET['navigate'] == "true"){
$_SESSION['email'] = $Email;
header('location: nextpage.php');
}
?>
所以我的页面看起来像是链接
user1
user2
user3
.
.
.
usern
我的问题是,当我单击任何链接时,它总是将第一封电子邮件存储在会话变量中。
所以,如果我在nextpage.php中输出SESSION
echo $_SESSION['email'];
它总是回显第一个链接email
。
我的猜测是因为我正在使用while循环,它总是会拾取第一个链接数据并保持不变,但是我的问题是我也该如何获取其他人的数据。我想导航到该用户页面,单击该用户页面的链接,只有当我在单击该链接时收到正确的电子邮件时,才能完成该操作。
正如您所说id
的不是主键,我假设您的主键id
将包含不同的键emails
,您必须执行以下更改:-
<?php
$msql=$db->prepare("SELECT * from users where id=? order by id desc");
$msql->bind_param("i",$user_id);
$result = $msql->execute(); // assign to a variable
//$msql = $msql->get_result(); //you are over-writing to one variable which is not correct
//$msql = $msql->num_rows; //you are over-writing to one variable which is not correct
while($usercount=$result->fetch_assoc()){
$Email = $usercount['email'];
$FirstName = $usercount['first_name'];
$LastName = $usercount['last_name'];
?>
<strong><a href="?navigate=true&email=$Email" class="post_user" id="post_user" data-emailID="<?php echo $Email; ?>"><?php echo $FirstName.' '.$LastName;?></a></strong> <!-- send mail id to the url too otherwise how's you will get the email id to save it into SESSION-->
<?php } ?>
和
<?php
if(isset($_GET['navigate']) && $_GET['navigate'] == "true"){
$_SESSION['email'][] = $_GET['email']; // assign each mail to SESSION ARRAY not SESSION variable
header('location: nextpage.php');
}
?>
和
echo "<pre/>";print_r($_SESSION['email']); //to see all emails.
注意:-如果要session_start();
在php
该页面上使用,<?php
则必须在页面顶部写上SESSION
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句