Search and Display User Profiles

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

meesh2175
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Feb 16, 2012 1:03 pm

Search and Display User Profiles

Postby meesh2175 » Mon Oct 15, 2012 11:54 am

Hello,

I am trying to setup a website that you can store and search for user profiles. I want to be able to search by last name, display the list of user(s) that match that last name and then be able to select one of those users from the list to display their full profile.

I have created a page called search_user.php that includes a form with one input textfield used for searching the last name.

search_user.php

Code: Select all

<form name="member" method="post" action="results_user.php">
Please enter the last name of the user:
<input name="search" type="text" id="search">
<input name="submit" type="submit" id="submit" value="Search" />
</form>


My next page displays a list of users that match that last name. Currently it echos their last name and first name. Their last name is a hyperlink which would ideally go to their full profile.

results_user.php

Code: Select all

<?php
if(isset($_POST['search']))
{
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Can't connect to database");
mysql_select_db(DB_DATABASE) or die(mysql_error());

#Convert to upper case, trim it, and replace spaces with "|":
$search = (ini_get('magic_quotes_gpc')) ? stripslashes($_POST['search']) :
$_POST['search'];
$search = mysql_real_escape_string($search);
$search = strtoupper(preg_replace('/\s+/', '|', trim($_POST['search'])));
 
#Create a MySQL REGEXP for the search:
$regexp = "REGEXP '[[:<:]]($search)[[:>:]]'";
 
$query = "SELECT * FROM `users` WHERE UPPER(`lastname`) $regexp";
$result = mysql_query($query) or die($query . " - " . mysql_error());
        
if(mysql_num_rows($result) <= 0){echo "No Results Found - Please try your search again.";}
  while($row = mysql_fetch_array($result))
  {   
     echo "<a href=profile_member.php>".$row['lastname']. "</a> ";
   echo $row['firstname'];
   echo "<br>";
  }
}
?>


What I am having trouble with is how to click on the last name and display that user's profile information from the mysql database?

Any help would be appreciated!

MeroD
New php-forum User
New php-forum User
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am

Re: Search and Display User Profiles

Postby MeroD » Mon Oct 15, 2012 11:23 pm

First create a new page to view the user profiles, if you haven't already...

Then: <a href="profile_member.php?id=' . $row['user_id'] . '">" . $row['lastname'] . "</a>...


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron