Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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;
?>

This is where the username changes to 'p'
PHP
<?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>

and this is where the id in a link changes to 'p'
PHP
<a href="profile?id=<?php echo htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8'); ?>">

then the users username and id changes to 'p' all throughout the site
Posted
Comments
Peter Leow 25-Nov-13 10:41am    
I notice the following, please verify, shouldn't the "</div></div></a>" be "</div></a></div>"?

$output .= "<div class='search_sex'>" . $row['sex'] . "</div></div></a>";

1 solution

In the while loop on the first $output you are not concatenating $row[id] but simply printing it. That could be it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900