Search and Display User Profiles

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

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

Mon Oct 15, 2012 11:54 am


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.


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" />
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.


Code: Select all

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']) : 
$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!

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

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>...

Post Reply