user9566654
user9566654

Reputation:

Going to another users page

I am creating a social networking site in PHP, HTML and MySQL . A feature on the site is users type in another users name and when they click on it, they go to that users wall . The problem I am having is that when I click on the users username after typing it in the search bar, I go back to my home page and not the other users home page . Can someone help me ?

search2.php:

include("connect.php");

$output = '';

if(isset($_POST['search'])) {

if (empty($_POST["searchh"])) {
echo"You didn't enter anything . ";
} else {

$searchq = $_POST['searchh'];
$searchq = preg_replace("#[^0-9a-z]#i", "",$searchq);

$searchq = mysqli_real_escape_string($conn, $_POST['searchh']);
$query = mysqli_query($conn ,"SELECT * FROM users WHERE username LIKE '%$searchq%'") or die("Could not search");    
$count = mysqli_num_rows($query);

 if($count == 0){

echo "User does not exists";

} else {

while($row = mysqli_fetch_array($query)) {
    $username = $row['username'];
    $id = $row['id'];
    $output .= '<a href="home.php?username=$username" >'. '<div>' .$username. '</div>' .'</a>';
}

}    
}
} else {
echo"Not working";

}

profile.php :

<center>
<form action="search2.php" method="post">
<input type="text" name="searchh" placeholder="Search user ...">
<button id="bt3" type="submit" name="search">Search</button>

</form>
</center>

Upvotes: 0

Views: 68

Answers (1)

Karlo Kokkak
Karlo Kokkak

Reputation: 3714

You're combining strings in this instance the wrong way.

Change:

'<a href="home.php?username=$username" >'

To:

'<a href="home.php?username='.$username.'" >'

For $_POST['searchh'] - typo? Should it be $_POST['search'] instead?

Also seems that at home.php, where a user's profile displays, username is being passed with extra whitespaces. So trim() call to incoming $_GET['username'] and to $_SESSION['username'] at the same time. Also home.php is not accepting incoming $_GET['username'] which is used to id which user to retrieve posts for.

Right after:

$username = isset($_SESSION['username']) ? $_SESSION['username'] : '';

Add:

$username = trim(isset($_GET['username']) ? $_GET['username'] : $username);

Upvotes: 2

Related Questions