link to specific user profile php
<p>I'm creating a search bar feature on my website where the user can search for users using a name.The search result may come up with multiple users with similar names (ex. if I search "Jenna", my database may have multiple users with the name "Jenna" so multiple results will show).I want the user to be able to click on one of the profiles and see that specific "Jenna's" user profile. Kind of like Twitter, where I can search for accounts and view different profiles. Right now I have code that returns the search and also makes the search result a clickable link. However, when I try to save the user id, it only saves the latest user id. </p>

<p>home.php (where the search bar for users is0</p>

<pre><code>&lt;form  method="GET" action="search.php"  id="searchform"&gt;
    Search for users:
    &lt;input  type="text" name="search_user" placeholder="Enter username"&gt;
    &lt;input  type="submit" name="submit" value="Search"&gt;
&lt;/form&gt;
</code></pre>

<p>search.php (prints out the users with the name that the user is searching for)</p>

<pre><code>session_start();
$user = '';
$password = '';
$db = 'userAccounts';
$host = 'localhost';
$port = 3306;
$link = mysqli_connect($host, $user, $password, $db);
mysqli_query($link,"GRANT ALL ON comment_schema TO 'oviya'@'localhost'");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$search_user = $_GET['search_user'];
$sql = "SELECT * FROM users WHERE username LIKE '%$search_user%'";
$result = mysqli_query($link, $sql);
if(mysqli_num_rows($result)&gt;0){
    while ($row = mysqli_fetch_assoc($result)) {
        $a = '&lt;a';
        $b = ' href="';
        $c = 'user_profiles.php';
        $d = '"&gt;';
        $e = $row['username'];
        $f = '&lt;/a';
        $g = '&gt;';
        $_SESSION['user'] = $row['user_id'];
        $userID = $_SESSION['user'];
        echo $a.$b.$c.$d.$e.$f.$g;
        header("Location: user_profiles.php");
    }
}
</code></pre>

<p>user_profiles.php (supposed to be where a specific user's profile is shown, based on the link the user clicks with the specific userID)</p>

<pre><code>session_start();
$userID=$_SESSION['user'];
$link = mysqli_connect('localhost', 'x', '', 'userAccounts');
$query="SELECT * FROM dataTable WHERE user_id='$userID'";
$results = mysqli_query($link,$query);
while ($row = mysqli_fetch_assoc($results)) {
    echo '&lt;div class="output" &gt;';
    $entry_id = $row["entry_id"];
    $output= $row["activity"];
    echo "Activity: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."&lt;br&gt;"."&lt;br&gt;";
    $output= $row["duration"];
    echo "Duration: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')." hrs"."&lt;br&gt;"."&lt;br&gt;";
    $output= $row["date_"];
    echo "Date: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."&lt;br&gt;"."&lt;br&gt;";
    echo '&lt;/div&gt;';
}
</code></pre>

<p>I get where my mistake is, the while loop in search.php will only save the latest userID so the link will always take me to the user profile with that useriD. I'm just not sure how to implement it so that when the user views the list of profiles, the link they click will take them to a specific profile based on the user id.  </p>