搜索某些内容后,用户名和ID会发生变化

时间:2013-11-23 22:26:21

标签: php search

我有一个小型社交网站,有个人资料页面和搜索页面。一切正常,在菜单栏上显示用户用户名,并通过profile.php?id=5显示其个人资料的链接(例如)。 然而,当我通过search.php搜索某些内容时,一切正常,但是当我在搜索后重新加载页面时,突然用户名显示为“p”,链接转到profile.php?id=p 有没有人知道发生了什么? 如果您需要更多信息,请告诉我,并提前致谢。 代码:

<?php

//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['search']);

//check whether the name parsed is empty
if($searchTerm == "")
{
    echo "Enter name you are searching for.";
    exit();
}

//database connection info
$host = ""; //server
$db = ""; //database name
$user = ""; //dabases user name
$pwd = ""; //password

//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);

//MYSQL search statement
$query = "SELECT * FROM users WHERE username SOUNDS LIKE '%$searchTerm%' or fname SOUNDS LIKE '%$searchTerm%' or lname SOUNDS LIKE '%$searchTerm%'";

$results = mysqli_query($link, $query);
echo "<div class='searched'>Results for ";
echo $searchTerm;
echo "</div>";
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
    $output = "";
    while($row = mysqli_fetch_array($results))
    {   
        $output .="<div class='user'><a href='profile?id=$row[id]'>";
        $output .= "<img class='search_pp' src='" . $row['picture'] . "'/><br>";
        $output .= "<div class='search_username'> " . $row['username'] . "</div>";
        $output .= "<div class='search_full'>Full name: " . $row['fname'] . " " .  $row['lname'] . "</div>";
        $output .= "<div class='search_sex'>" . $row['sex'] . "</div></div></a>";
    }
    echo $output;
}
else
    echo "No records of " . $searchTerm; 
?>

这是用户名变为'p'

的地方
<?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>

这是链接中的ID更改为“p”

的位置
<a href="profile?id=<?php echo htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8'); ?>">

然后用户的用户名和ID在整个网站中都变为'p'

1 个答案:

答案 0 :(得分:0)

1 - 您是否设置了$_SESSION['user']

2 - 你可以在刷新之前和之后做var_dump($_SESSION['user']);看看发生了什么事吗?