简体   繁体   中英

link to specific user profile php

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.

home.php (where the search bar for users is0

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

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

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)>0){
    while ($row = mysqli_fetch_assoc($result)) {
        $a = '<a';
        $b = ' href="';
        $c = 'user_profiles.php';
        $d = '">';
        $e = $row['username'];
        $f = '</a';
        $g = '>';
        $_SESSION['user'] = $row['user_id'];
        $userID = $_SESSION['user'];
        echo $a.$b.$c.$d.$e.$f.$g;
        header("Location: user_profiles.php");
    }
}

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)

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 '<div class="output" >';
    $entry_id = $row["entry_id"];
    $output= $row["activity"];
    echo "Activity: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
    $output= $row["duration"];
    echo "Duration: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')." hrs"."<br>"."<br>";
    $output= $row["date_"];
    echo "Date: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
    echo '</div>';
}

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.

You need to do changes in search and user.php files :

Search.php :

<?php
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) > 0) {

    while ($row = mysqli_fetch_assoc($result)) {
        $id = $row['user_id'];
        ?>
        <a href="user_profiles.php?id=<?php echo $id; ?>" >
            <?php echo $row['username']; ?>
        </a>
        <?php
        $_SESSION['user'] = $row['user_id'];
        $userID = $_SESSION['user'];

        header("Location: user_profiles.php");
    }
}

User_profile.php:

$userid = $_GET['id'];

$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 '<div class="output" >';
    $entry_id = $row["entry_id"];
    $output = $row["activity"];
    echo "Activity: ";
    echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8') . "<br>" . "<br>";
    $output = $row["duration"];
    echo "Duration: ";
    echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8') . " hrs" . "<br>" . "<br>";
    $output = $row["date_"];
    echo "Date: ";
    echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8') . "<br>" . "<br>";
    echo '</div>';
}

Very first thing, you are saving multiple user ids to a string.

Another thing, you are saving it in while loop.

Therefore, latest value updates old value.

In your case, it will always save the last value. That is prime issue.

You can take array of user ids and save them in it.

$userIds = array();
while ($row = mysqli_fetch_assoc($result)) {
        $a = '<a';
        $b = ' href="';
        $c = 'user_profiles.php';
        $d = '">';
        $e = $row['username'];
        $f = '</a';
        $g = '>';
        $userIds[] = $row['user_id'];
        $userID = $_SESSION['user'];
        echo $a.$b.$c.$d.$e.$f.$g;
        header("Location: user_profiles.php");
    }
$_SESSION['user'] = $userIds;

And in your user_profiles.php , loop over the array or use MySQL IN() condition to get all user profiles.

Also, why did you take too many variables for html link. You can do it in single variable using concatenation like following:

$userIds = array();
while ($row = mysqli_fetch_assoc($result)) {
 $a = '<a'
 . ' href="';
 . 'user_profiles.php';
 . '">';
 . $row['username'];
 . '</a';
 . '>';
 $userIds[] = $row['user_id'];
 $userID = $_SESSION['user'];
 echo $a;
 header("Location: user_profiles.php");
}
$_SESSION['user'] = $userIds;

Another mistake is that you are echo ing HTML link and doing redirection.

That will cause headers already sent... error.

This will display list of users with searched string

if(mysqli_num_rows($result)>0){

    while ($row = mysqli_fetch_assoc($result)) {
$link="<a href='user_profiles.php?user_id=".$row['user_id']."'>".$row['username']."</a>";
    }
}

After clicking on link it will redirect to user_profiles.php (no need to header. header is used for automatic redirection) In user_profiles.php

session_start();
$userID=$_GET['user_id'];

$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 '<div class="output" >';
    $entry_id = $row["entry_id"];
    $output= $row["activity"];
    echo "Activity: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
    $output= $row["duration"];
    echo "Duration: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')." hrs"."<br>"."<br>";
    $output= $row["date_"];
    echo "Date: ";
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
    echo '</div>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM