mysql - link to specific user profile php -
i'm creating search bar feature on website user can search users using name.the search result may come multiple users similar names (ex. if search "jenna", database may have multiple users name "jenna" multiple results show).i want user able click on 1 of profiles , see specific "jenna's" user profile. kind of twitter, can search accounts , view different profiles. right have code returns search , makes search result clickable link. however, when try save user id, saves latest user id.
home.php (where search bar users is0
<form method="get" action="search.php" id="searchform"> search users: <input type="text" name="search_user" placeholder="enter username"> <input type="submit" name="submit" value="search"> </form>
search.php (prints out users name user searching for)
session_start(); $user = ''; $password = ''; $db = 'useraccounts'; $host = 'localhost'; $port = 3306; $link = mysqli_connect($host, $user, $password, $db); mysqli_query($link,"grant on comment_schema 'oviya'@'localhost'"); if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); } $search_user = $_get['search_user']; $sql = "select * users username '%$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 specific user's profile shown, based on link user clicks specific userid)
session_start(); $userid=$_session['user']; $link = mysqli_connect('localhost', 'x', '', 'useraccounts'); $query="select * datatable 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 mistake is, while loop in search.php save latest userid link take me user profile userid. i'm not sure how implement when user views list of profiles, link click take them specific profile based on user id.
you need changes in search , 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 on comment_schema 'oviya'@'localhost'"); if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); } $search_user = $_get['search_user']; $sql = "select * users username '%$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 * datatable 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>'; }
Comments
Post a Comment