搜索某些内容后,用户名和ID会发生变化 [英] Username and ID change after i search for something

查看:72
本文介绍了搜索某些内容后,用户名和ID会发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型社交网站,有个人资料页面和搜索页面。一切正常,在菜单栏上显示用户名,并通过`profile.php?id = 5`(例如)显示其个人资料的链接。

当我搜索某些内容时通过`search.php`它一切正常,但是当我在搜索后重新加载页面时,突然用户名显示为'p',链接转到`profile.php?id = p`

有没有人知道发生了什么事?

如果您需要更多信息,请告诉我,并提前致谢。

代码:

I have a small social website, with profile pages and a search page. It all works fine and on the menu bar it displays the users username with a link to their profile via `profile.php?id=5` (for example).
How ever when I search for something via `search.php` it all works fine, but then when I reload a page after searching, suddenly the username is displayed as 'p' and the link goes to `profile.php?id=p`
Does anybody have any idea what's happening?
Tell me if you need any more information, and thanks in advance.
The code:

<?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'的地方


This is where the username changes to 'p'

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



这是链接中的ID更改为p的位置


and this is where the id in a link changes to 'p'

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



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


then the users username and id changes to 'p' all throughout the site

推荐答案

searchTerm = trim(
searchTerm = trim(


_GET [' 搜索]);

// 检查解析的名称是否为空
if (
_GET['search']); //check whether the name parsed is empty if(


searchTerm ==
{
echo 输入您要搜索的名称为
exit();
}

// 数据库连接信息
searchTerm == "") { echo "Enter name you are searching for."; exit(); } //database connection info


这篇关于搜索某些内容后,用户名和ID会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆