Display user's profile with image on search in php

Rahul Ranjan
 7th Jan, 2020
  120 Comments
Display user's profile with image on search in php

Hi everyone, today's article is mostly the advanced version of my previous article  Creating user profile Page using PHP and Mysql . In this article we are going to learn about how to create user profile page with implementation of search, through which one can search other registered user. 

Here firstly we will create a utility file, which will help in accessing database and will contain some reusable important function. So, just create a file named function.php and put content below into that:

<?php 
$mysqli_hostname  =  "host";
$mysqli_user  =  "user";
$mysqli_password  =  "password";
$mysqli_database  =  "database";
$con = mysqli_connect($mysqli_hostname,$mysqli_user,$mysqli_password,$mysqli_database);
if(!$con){
	echo "Database Connection failed!";
	exit();
}
function session_value($val){
	if($val=="id"){
		$res=$_SESSION['user_id'];
	}elseif($val=="username"){
		$res=$_SESSION['username'];
	}elseif($val=="login"){
		$res=$_SESSION['login_user'];
	}
	return $res;
}
function user_check($page){
	if($page=="home"){
		if(length(session_value("login"))!='1'){
			header("location:  welcome.php");
		}
	}if($page==""){
		$ss=length(session_value("login"));
		if(length(session_value("login"))=='1'){
			header("location:  index.php?remark_login=failed");
		}
	}
}
function session_check($arg){
	if($arg=="formaccess"){
	$x=$_SESSION['login_user'];
	$len=length($x);
	if($len!="1"){
		header("location:  ./dashboard/index.php?remark=previous_session_error");
	}
	}
	if($arg==""){
		$_SESSION['user_id']="";
		session_start();
		if($_SESSION['user_id']==0){
			header("location:  ./index.php?goback");
		}
	}
	}
function user_session(){
	global $con,$prefix;
	session_start();
	if(isset($_SESSION['user_id']) && $_SESSION['user_id'] != ""){}else{
	$x=substr(str_shuffle('23456789'),0,4);
	$_SESSION['user_id']=$x;
	}
	if(isset($_SESSION['username']) && $_SESSION['username'] != ""){}else{
		$x=substr(str_shuffle('ABCDEFRTOPICS23456789'),0,5);
		$_SESSION['username']="Guest_".$x."";
	}
	if(isset($_SESSION['login_user']) && $_SESSION['login_user'] != ""){}else{
		$x=substr(str_shuffle('abcdefrtopics23456789'),0,1);
		$_SESSION['login_user']=$x;
	}
}

function length($get){
		$x=strlen($get);
		return $x;
	}
function user_info($id,$value="name"){
	global $con;
	$sql=mysqli_query($con,"Select * from member where mem_id='$id'");
	$row=mysqli_fetch_array($sql);
	if($value=="name"){
		$rs=$row['name'];
	}if($value=="username"){
		$rs=$row['username'];
	}if($value=="email"){
		$rs=$row['email'];
	}if($value=="dob"){
		$rs=$row['dob'];
	}if($value=="profession"){
		$rs=$row['profession'];
	}if($value=="gender"){
		$rs=$row['gender'];
	}if($value=="location"){
		$rs=$row['location'];
	}
	if($value=="image_check"){
		$rs=$row['image_url'];
	}
	if($value=="image"){
		if($row['image_url']!=""){
		$rs=$row['image_url'];
		}else{
		$rs="upload/user.png";		
		}
	}if($value=="image_url"){
		if($row['image_url']!=""){
		$rs="upload/pic_".$row['image_url'];
		}else{
		$rs="upload/user.png";		
		}
	}if($value=="image_thumb"){
		if($row['image_url']!=""){
		$rs="upload/thumb_".$row['image_url'];
		}else{
		$rs="upload/user.png";		
		}
	}
	if($value=="website"){
		$rs=$row['website'];
	}if($value=="about_me"){
		$rs=$row['about_me'];
	}
	return $rs;
}
?>

Now we will create table to store the user's details. To create table we will import below contents of table.sql into our phpmyadmin or directly you can also run below query in sql query window of phpmyadmin .

table.sql

CREATE TABLE IF NOT EXISTS `members` (
 `mem_id` int(11) NOT NULL AUTO_INCREMENT,
 `username` varchar(30) NOT NULL,
 `password` varchar(120) NOT NULL,
 `email` varchar(150) NOT NULL,
 `name` varchar(50) NOT NULL,
 `dob` varchar(21) NULL,
 `profession` varchar(120) NULL,
 `gender` varchar(20) NULL,
 `location` text NULL,
 `image_url` varchar(250) NULL,
 `website` varchar(150) NULL,
 `about_me` text NULL,
 PRIMARY KEY (`mem_id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

In order to collect user data we need a registration form and to check the same detail in order to allow user to access secure page we will create a login form. Copy and paste below code into your index page.

index.php

<?php
include  "function.php";
user_session();
user_check("home");
?>
<!DOCTYPE html>
<html>
<head>
<meta  content='text/html;  charset=UTF-8'  http-equiv='Content-Type'/>
<link rel="stylesheet" type="text/css" href="custom.css"/>
<script src="asset/js/jquery.2.1.3.min.js" type="text/javascript"> </script>
<script src="script.js" type="text/javascript"> </script>
</head>
<body>
<div class="tp-container">
<div class="tp-boxwrap">
<header>
<div class="tp-logo">7topics Demo</div>
</header>
<nav class="tp-nav">
<ul>
	<li><a href="http://7topics.com">Home</a></li>
	<li><a href="https://7topics.com/display-users-profile-with-image-on-search-in-php.html">Tutorial</a></li>
</ul>
</nav>
<div class="tp-content">
	<div class="wt_99 center">
	 <div class="navigator left">
		<ul>
		<li><a href="index.php">HOME</a></li>
		<span> &raquo; </span>
		<li>Display user image on search in php.</li>
		</ul>
	 </div>
	</div>
	<div class="wt_99 center left">
	 <div class="right search_form">
		<form action="search.php" method="post">
			<input type="text" name="user" placeholder="Search.."/>
			<input type="submit" name="submit" value="Search"/>
		</form>
	 </div>
	</div>
	<div class="tp-contentbox">
		<div class="tp-contentwrap1">
		<button class="tp-btn tp-btnstyle active" id="b1" onclick='tabtoggle("t1");'>LOGIN</button>
		<button class="tp-btn tp-btnstyle inactive" id="b2" onclick='tabtoggle("t2");'>SIGNUP</button>
		</div>
		<div class="tp-contentwrap2">
			<div class="" id="t1">
				<?php
					$remarks  =  isset($_GET['remark_login'])  ?  $_GET['remark_login']  :  '';
					if  ($remarks==null  and  $remarks=="")
					{
						echo  '<span class="tp-regtext eee">Login Here</span>';
					}
					if  ($remarks=='field_empty')
					{
						echo '<script>window.onload=tabtoggle("t1");</script>';
						echo  '<span class="tp-regtext lightred">Fill all the fields.</span>';
					}
					if  ($remarks=='failed')
					{
						echo '<script>window.onload=tabtoggle("t1");</script>';
						echo  '<span class="tp-regtext lightred">Login  Failed!,  Invalid  Credentials</span>';
					}
				?>
				<form  action="logincheck.php"  method="post"  >
				<p><label for="username">Username: </label><input class="right" type="text" name="username" placeholder="Username"/> </p>
				<p><label for="password">Password: </label><input class="right" type="password" name="password" placeholder="password"/> </p>
				<p><button class="tp-btn create pointer" type="button" onclick='tabtoggle("t2");'>Create an account</button></p>
				<center><input type="submit" name="submit" class="tp-btn submit pointer" value="Login"></center>
				</form>
			</div>
			<div class="" id="t2">
				<?php  
				$remarks  =  isset($_GET['remarks'])  ?  $_GET['remarks']  :  '';
				if  ($remarks==null  and  $remarks=="")
				{
					echo  '<span class="tp-regtext eee">New User! Register Here</span>';
				}
				if  ($remarks=='success')
				{
					echo '<script>window.onload=tabtoggle("t2");</script>';
					echo  '<span class="tp-regtext green">Registration  Success</span>';
				}
				if  ($remarks=='failed')
				{
					echo '<script>window.onload=tabtoggle("t2");</script>';
					echo  '<span class="tp-regtext lightred">Registration  Failed!,  Username  exists</span>';
				}if  ($remarks=='password_mismatch')
				{
					echo '<script>window.onload=tabtoggle("t2");</script>';	
					echo  '<span class="tp-regtext lightred">Both Password must be same</span>';
				}if  ($remarks=='field_empty')
				{
					echo '<script>window.onload=tabtoggle("t2");</script>';
					echo  '<span class="tp-regtext lightred">Fill all the Fields.</span>';
				}if  ($remarks=='invalid_email')
				{
					echo '<script>window.onload=tabtoggle("t2");</script>';
					echo  '<span class="tp-regtext lightred">Invalid Email Address.</span>';
				}if  ($remarks=='error')
				{
					echo '<script>window.onload=tabtoggle("t2");</script>';
					echo  '<span class="tp-regtext lightred">Registration failed<br>Error: '.$_GET['remarks'].'</span>';
				}
				?>
				<form  name="reg"  action="execute.php" method="post"  id="reg">
				<p><label for="name">Name: </label><input class="right" type="text" name="name" placeholder="Name"/> </p>
				<p><label for="username">Username: </label><input class="right" type="text" name="username" placeholder="Username"/> </p>
				<p><label for="password">Password: </label><input class="right" type="password" name="password" placeholder="password"/> </p>
				<p><label for="password">Repeat Password: </label><input class="right" type="password" name="passwordrepeat" placeholder="Retype password"/> </p>
				<p><label for="email">Email: </label><input class="right" type="text" name="email" placeholder="yourname@mail.com"/> </p>
				<center><input type="submit" name="submit" class="tp-btn submit pointer" value="Register Me"/></center>
				</form>
			</div>
		</div>
	</div>
</div>
<footer>
	<div class="">&copy; Copyright 7topics.com 2015-20.</div>
</footer>
</div>
</div>

</body>
</html>

 Now we will match the login data submitted by user with data stored in our database. For that create new file named logincheck.php and paste content given below:

logincheck.php

<?php
include "function.php";
session_start();
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=md5($_POST['password']);
$result  =  mysqli_query($con,"SELECT  *  FROM  member where username='$username' and password='$password'");
$c_rows  =  mysqli_num_rows($result);

if  ($c_rows<1)  {
header("location:  index.php?remark_login=failed");
}
if($username=="" || $password==""){
header("location:  index.php?remark_login=field_empty");
}
$sql="SELECT  *  FROM  member  WHERE  username='$username'  and  password='$password'";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_array($result);
$count=mysqli_num_rows($result);
if($count==1)
{
if(isset($_SESSION['user_id']) && $_SESSION['user_id'] != ""){
$y=$row['mem_id'];
$_SESSION['user_id']=$row['mem_id'];
}else{
$_SESSION['user_id']=$row['mem_id'];
}
if(isset($_SESSION['username']) && $_SESSION['username'] != ""){
$x=$row['username'];
$_SESSION['username']=$row['username'];
	}
else{
	$_SESSION['username']=$row['username'];
}
if(isset($_SESSION['login_user']) && $_SESSION['login_user'] != ""){
	$x=substr(str_shuffle('abcdefrtopics23456789'),0,3);
	$_SESSION['login_user']=$x;
}else{
	$x=substr(str_shuffle('abcdefrtopics23456789'),0,3);
	$_SESSION['login_user']=$x;
}
if(isset($_SESSION['time']) && $_SESSION['time'] != ""){}else{
	$_SESSION['time']=time();
}
header("location:  ./welcome.php?remark_login=login_success");
}
}else{	
header("location:  ./index.php?remark_login=access_denied");
}
?>

logout.php

<?php
session_start();
if(session_destroy())
{
header("Location:  index.php");
}else{
header("Location:  index.php");
}
?>

 Create a file named execute.php, where we will be storing the user's submitted data:

execute.php

<?php
session_start();
include('function.php');

$name=$_POST['name'];
$email=$_POST['email'];
$username=$_POST['username'];
$password=md5($_POST['password']);
$passwordrepeat=md5($_POST['passwordrepeat']);
$result  =  mysqli_query($con,"SELECT  *  FROM  member  WHERE  username='$username'");
$num_rows  =  mysqli_num_rows($result); 
if($name=="" ||$email=="" ||$username=="" ||$password=="" ||$passwordrepeat=="" ){
	header("location:  index.php?remarks=field_empty");
}elseif($password!=$passwordrepeat)  {
	header("location:  index.php?remarks=password_mismatch");
}elseif($num_rows){
	header("location:  index.php?remarks=failed");
}elseif(!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) { 
	header("location:  index.php?remarks=invalid_email");
}else{
	if(mysqli_query($con,"INSERT  INTO  member(name,  email,username,  password)VALUES('$name',  '$email','$username',  '$password')")){
		header("location:  index.php?remarks=success");
	}else{
		$e=mysqli_error($con);
		header("location:  index.php?remarks=error&value=$e");		
	}
}
?>

Now we will go with Welcome page that any logged in user will visit after login.

welcome.php

<?php
include('function.php');
user_session();
user_check("");
?>
<!DOCTYPE html>
<html>
<head>
<meta  content='text/html;  charset=UTF-8'  http-equiv='Content-Type'/>
<link rel="stylesheet" type="text/css" href="welcome.css"/>
<script src="asset/js/jquery.2.1.3.min.js" type="text/javascript"> </script>
<script src="script.js" type="text/javascript"> </script>
</head>
<body>
<div class="tp-container">
<div class="tp-boxwrap">
<header>
<div class="tp-logo">7topics Demo</div>
</header>
<nav class="tp-nav">
<ul>
	<li><a href="http://7topics.com">Home</a></li>
	<li><a href="https://7topics.com/display-users-profile-with-image-on-search-in-php.html">Tutorial</a></li>
</ul>
</nav>
<div class="tp-content">
	<div class="wt_99 center">
	 <div class="navigator left">
		<ul>
		<li><a href="index.php">HOME</a></li>
		<span> &raquo; </span>
		<li>Display user image on search in php.</li>
		</ul>
	 </div>
	</div>
	<div class="wt_99 center left">
	 <div class="right search_form">
		<form action="search.php" method="post">
			<input type="text" name="user" placeholder="Search.."/>
			<input type="submit" name="submit" value="Search"/>
		</form>
	 </div>
	</div>
	<div class="tp-contentbox">
	<div class="tp-infostrip">
		<nav>
		<ul>
			<li class="left"><a href="welcome.php">Profile</a></li>
			<li class="left"><a href="logout.php">Logout</a></li>
			<li class="right"><a href="welcome.php">Hello <?php  echo  session_value("username");  ?></a></li>
		<ul>
		</nav>			
	</div>
	<div class="tp-contentwrap1">
		<div class="left wt_25 side_1">
			
			<div class="image_block">
			<img src="<?php echo user_info(session_value("id"),"image_url");?>"/>
			</div>
			<div class="menu_list">
				<li><a href="edit_profile.php">Edit Profile</a></li>
				<li><a href="deleteac.php">Delete Account</a></li>				
			</div>
		</div>
		<div class="right wt_75">
			<?php
					$id=session_value("id");
					$sql="SELECT  *  FROM  member  where  mem_id='$id'";
					$result=mysqli_query($con,$sql);
					$rows=mysqli_fetch_array($result);
			?>
			<div class="tp-contentwrap2">
			<div class="strip-profile">General Info</div>
			<table>
			<tr>
				<td class="td_1">Username :</td>
				<td  class="left"><?php  echo  $rows['username'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Name :</td>
				<td  class="left"><?php  echo  $rows['name'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Email id :</td>
				<td  class="left"><?php  echo  $rows['email'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Date Of Birth :</td>
				<td  class="left"><?php  echo  $rows['dob'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Gender :</td>
				<td  class="left"><?php  echo  $rows['gender'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Profession :</td>
				<td  class="left"><?php  echo  $rows['profession'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Location :</td>
				<td  class="left"><?php  echo  $rows['location'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Website :</td>
				<td  class="left"><?php  echo  $rows['website'];  ?></td>
			</tr>
			</table>
			<div class="strip-profile">About me</div>
			<div class="wt_99 left">
			<span  class="left about"><?php  echo  $rows['about_me'];  ?></span>
			</div>
			</div>
			</div>
		</div>
			
</div>

</div>
<footer>
	<div class="">&copy; Copyright 7topics.com 2015-20.</div>
</footer>
</div>
</div>

</body>
</html>

We will now create page to help user edit their profile. Copy and paste code given below:

edit_profile.php

<?php
include('function.php');
user_session();
user_check("");
?>
<!DOCTYPE html>
<html>
<head>
<meta  content='text/html;  charset=UTF-8'  http-equiv='Content-Type'/>
<link rel="stylesheet" type="text/css" href="edit_profile.css"/>
<script src="asset/js/jquery.2.1.3.min.js" type="text/javascript"> </script>
<script src="script.js" type="text/javascript"> </script>
</head>
<body>
<div class="tp-container">
<div class="tp-boxwrap">
<header>
<div class="tp-logo">7topics Demo</div>
</header>
<nav class="tp-nav">
<ul>
	<li><a href="http://7topics.com">Home</a></li>
	<li><a href="https://7topics.com/display-users-profile-with-image-on-search-in-php.html">Tutorial</a></li>
</ul>
</nav>
<div class="tp-content">
	<div class="wt_99 center">
	 <div class="navigator left">
		<ul>
		<li><a href="index.php">HOME</a></li>
		<span> &raquo; </span>
		<li>Display user image on search in php.</li>
		</ul>
	 </div>
	</div>
	<div class="wt_99 center left">
	 <div class="right search_form">
		<form action="search.php" method="post">
			<input type="text" name="user" placeholder="Search.."/>
			<input type="submit" name="submit" value="Search"/>
		</form>
	 </div>
	</div>
	<div class="tp-contentbox">
	<div class="tp-infostrip">
		<nav>
		<ul>
			<li class="left"><a href="welcome.php">Profile</a></li>
			<li class="left"><a href="logout.php">Logout</a></li>
			<li class="right"><a href="welcome.php">Hello <?php  echo  session_value("username");  ?></a></li>
		<ul>
		</nav>			
	</div>
	<div class="tp-contentwrap1">
		<div class="left wt_25 side_1">
			
			<div class="image_block">
			<img src="<?php echo user_info(session_value("id"),"image_url");?>"/>
			</div>
			<div class="menu_list">
				<li><a href="edit_profile.php">Edit Profile</a></li>
				<li><a href="deleteac.php">Delete Account</a></li>				
			</div>
		</div>
		<div class="right wt_75">
			<?php
					$var=session_value("id");
					$sql="SELECT  *  FROM  member  where  mem_id=$var";
					$result=mysqli_query($con,$sql);
					$rows=mysqli_fetch_array($result);
			?>
			<div class="tp-contentwrap2">
			<div class="strip-profile">Edit Profile</div>
			<?php
					$remarks  =  isset($_GET['remark_login'])  ?  $_GET['remark_login']  :  '';
					if  ($remarks==null  and  $remarks=="")
					{}
					if  ($remarks=='invalid_email')
					{
						echo  '<span class="tp-regtext lightred">Invalid E-mail.</span>';
					}if  ($remarks=='name')
					{
						echo  '<span class="tp-regtext lightred">Enter Your Name.</span>';
					}
				?>
			<form action="updateac.php" method="post" enctype="multipart/form-data">
			<table>
			<tr>
				<td class="td_1">Image :</td>
				<td  class="right"><input type="file" name="image_file"/></td>
			</tr>
			<tr>
				<td class="td_1">Name :</td>
				<td  class="right"><input type="text" name="name" value="<?php  echo  $rows['name'];  ?>" placeholder="name"/></td>
			</tr>
			<tr>
				<td class="td_1">Email id :</td>
				<td  class="right"><input type="text" name="email" value="<?php  echo  $rows['email'];  ?>" placeholder="email"/></td>
			</tr>
			<tr>
				<td class="td_1">Date Of Birth :</td>
				<td  class="right"><input type="text" name="dob" value="<?php  echo  $rows['dob'];  ?>" placeholder="dd/mm/yyyy"/></td>
			</tr>
			<tr>
				<td class="td_1">Gender :</td>				
				<?php  $sad=$rows['gender']; if($sad=="male"){$rs="selected";}else{$rs="";}if($sad=="female"){$acer="selected";}else{$acer="";}  ?>
				<td  class="right">
				<select name="gender">
					<option value="male" <?php echo $rs;?> >Male</option>
					<option value="female" <?php echo $acer;?> >Female</option>
				</select>	
				</td>
			</tr>
			<tr>
				<td class="td_1">Profession :</td>
				<td  class="right"><input type="text" name="profession" value="<?php  echo  $rows['profession'];  ?>" placeholder="profession"/></td>
			</tr>
			<tr>
				<td class="td_1">Location :</td>
				<td  class="right">				
					<textarea name="location" placeholder="location..."><?php  echo  $rows['location']; ?></textarea>
				</td>
			</tr>
			<tr>
				<td class="td_1">Website :</td>
				<td  class="right"><input type="text" name="website" value="<?php  echo  $rows['website'];  ?>" placeholder="website"/></td>
			</tr>
			<tr>
				<td class="td_1">About Me :</td>
				<td  class="right">
					<textarea name="about_me" placeholder="About me..."  rows="2" cols="25"><?php  echo  $rows['about_me'];?></textarea>
				</td>
			</tr>
			<tr>
				<th colspan="2"><input type="submit" name="submit" value="submit"/></th>
			</tr>
			</table>
			</form>
			</div>
			</div>
		</div>
			
</div>

</div>
<footer>
	<div class="">&copy; Copyright 7topics.com 2015-20.</div>
</footer>
</div>
</div>

</body>
</html>

Here we will display Image of users and allow them to change their profile pic. I am using a image plugin(ImageManipulator.php) written in php to easily handle any image. You can download it from here  and include it in your project.

updateac.php

<?php 
include('function.php');
user_session();
if(isset($_POST['submit'])){
$id=session_value("id");
$name=$_POST['name'];
$email=$_POST['email'];
$dob=$_POST['dob'];
$gender=$_POST['gender'];
$profession=$_POST['profession'];
$location=$_POST['location'];
$website=$_POST['website'];
$about_me=$_POST['about_me'];
$image_row=user_info($id,"image_check");
if(!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) { 
	header("location:  edit_profile.php?remarks=invalid_email");
}elseif($name=="") { 
	header("location:  edit_profile.php?remarks=invalid_email");
}else{
	require_once('ImageManipulator.php');
    $validExtensions = array('.jpg', '.jpeg', '.gif', '.png','.JPG', '.JPEG', '.GIF', '.PNG');
    $fileExtension = strrchr($_FILES['image_file']['name'], ".");
    if (in_array($fileExtension, $validExtensions)) {
        $newNamePrefix = $id."_".time() . '_';
        $manipulator = new ImageManipulator($_FILES['image_file']['tmp_name']);
        $width  = $manipulator->getWidth();
        $height = $manipulator->getHeight();		
        $newImage = $manipulator->resample(212,200,false);//Resizing Image to fit in profile     
        $manipulator->save('upload/pic_'.$newNamePrefix.$_FILES['image_file']['name']);// saving  image to upload folder
        $newImage = $manipulator->resample(120,120,false);//Shop resize 415px x 321px 
        $manipulator->save('upload/thumb_'.$newNamePrefix.$_FILES['image_file']['name']);// saving pic thumbnail to upload folder
		$pic_url="".$newNamePrefix.$_FILES['image_file']['name']."";
        echo '<p style="color:red">success ...</p>';		
    } else {
        echo '<p style="color:red">You must upload an image...</p>';
    }
	if($pic_url==""){
		$pic_url=user_info($id,"image");
	}else{
		$get_image=user_info($id,"image_check");
		$image_url="upload/pic_".$get_image;
		$image_thumb="upload/thumb_".$get_image;
		unlink($image_url);
		unlink($image_thumb);
	}
	if(mysqli_query($con,"Update member set image_url='$pic_url',name='$name',email='$email', dob='$dob',gender='$gender',profession='$profession',  location='$location',
	website='$website', about_me='$about_me' where mem_id='$id'")){
		header("location:  welcome.php?remarks=updated");
	}else{
		$e=mysqli_error($con);
		header("location:  welcome.php?remarks=error&value=$e");
	}	
}	
}else{
header("location:  welcome.php?remarks=access_denied");	
}

?>

As our main aim is to allow user to search others profile in this tutorial. We will add script to process our search query. Copy and paste following code in search.php:

search.php

<?php
include('function.php');
user_session();
user_check("");
?>
<!DOCTYPE html>
<html>
<head>
<meta  content='text/html;  charset=UTF-8'  http-equiv='Content-Type'/>
<link rel="stylesheet" type="text/css" href="search.css"/>
<script src="asset/js/jquery.2.1.3.min.js" type="text/javascript"> </script>
<script src="script.js" type="text/javascript"> </script>
</head>
<body>
<div class="tp-container">
<div class="tp-boxwrap">
<header>
<div class="tp-logo">7topics Demo</div>
</header>
<nav class="tp-nav">
<ul>
	<li><a href="http://7topics.com">Home</a></li>
	<li><a href="https://7topics.com/display-users-profile-with-image-on-search-in-php.html">Tutorial</a></li>
</ul>
</nav>
<div class="tp-content">
	<div class="wt_99 center">
	 <div class="navigator left">
		<ul>
		<li><a href="index.php">HOME</a></li>
		<span> &raquo; </span>
		<li>Display user image on search in php.</li>
		</ul>
	 </div>
	</div>
	<div class="wt_99 center left">
	 <div class="right search_form">
		<form action="search.php" method="post">
			<input type="text" name="user" placeholder="Search.."/>
			<input type="submit" name="submit" value="Search"/>
		</form>
	 </div>
	</div>
	<div class="tp-contentbox">
	<div class="tp-infostrip">
		<nav>
		<ul>
			<li class="left"><a href="welcome.php">Profile</a></li>
			<li class="left"><a href="logout.php">Logout</a></li>
			<li class="right"><a href="welcome.php">Hello <?php  echo  session_value("username");  ?></a></li>
		<ul>
		</nav>			
	</div>
	<div class="tp-contentwrap1">
		<div class="left wt_25 side_1">
			
			<div class="image_block">
			<img src="<?php echo user_info(session_value("id"),"image_url");?>"/>
			</div>
			<div class="menu_list">
				<li><a href="edit_profile.php">Edit Profile</a></li>
				<li><a href="deleteac.php">Delete Account</a></li>				
			</div>
		</div>
		<div class="right wt_75">
			<?php
					$id=session_value("id");
					$user=$_POST['user'];
					$sql="SELECT  *  FROM  members  where name LIKE '%$user%' and username LIKE '%$user%'";
					$result=mysqli_query($con,$sql);
			?>
			<div class="tp-contentwrap2">
			<div class="strip-profile">You searched for terms related to "<?php echo $user;?>"</div>
			<table>
			<?php 
			while($rows=mysqli_fetch_array($result)){	
				$id=$rows['mem_id'];
			?>
			<tr>
				<td class="td_1">
					<?php $image=user_info($id,"image_thumb");?>	
					<a href="profile.php?id=<?php echo $id;?>" target="_blank"><img src="<?php echo $image;?>" width="50px"/></a>
				</td>
				<td  class="left"><a href="profile.php?id=<?php echo $id;?>" target="_blank"><?php  echo  $rows['username'];  ?></a></td>
			</tr>
			<?php 
			}
			?>
			</table>
			</div>
			</div>
		</div>
			
</div>

</div>
<footer>
	<div class="">&copy; Copyright 7topics.com 2015-20.</div>
</footer>
</div>
</div>

</body>
</html>

When any user will click on user image or username displayed on search.php he will be redirected to his profile. To make it happen we will create a new page called profile.php :

profile.php

<?php
include('function.php');
user_session();
user_check("");
?>
<!DOCTYPE html>
<html>
<head>
<meta  content='text/html;  charset=UTF-8'  http-equiv='Content-Type'/>
<link rel="stylesheet" type="text/css" href="profile.css"/>
<script src="asset/js/jquery.2.1.3.min.js" type="text/javascript"> </script>
<script src="script.js" type="text/javascript"> </script>
</head>
<body>
<div class="tp-container">
<div class="tp-boxwrap">
<header>
<div class="tp-logo">7topics Demo</div>
</header>
<nav class="tp-nav">
<ul>
	<li><a href="http://7topics.com">Home</a></li>
	<li><a href="https://7topics.com/display-users-profile-with-image-on-search-in-php.html">Tutorial</a></li>
</ul>
</nav>
<div class="tp-content">
	<div class="left tp-content">
	<div class="wt_99 center">
	 <div class="navigator left">
		<ul>
		<li><a href="index.php">HOME</a></li>
		<span> &raquo; </span>
		<li>Display user image on search in php.</li>
		</ul>
	 </div>
	</div>
	<div class="wt_99 center left">
	 <div class="right search_form">
		<form action="search.php" method="post">
			<input type="text" name="user" placeholder="Search.."/>
			<input type="submit" name="submit" value="Search"/>
		</form>
	 </div>
	</div>
	<div class="tp-contentbox">
	<div class="tp-infostrip">
		<nav>
		<ul>
			<li class="left"><a href="welcome.php">Profile</a></li>
			<li class="left"><a href="logout.php">Logout</a></li>
			<li class="right"><a href="welcome.php">Hello <?php  echo  session_value("username");  ?></a></li>
		<ul>
		</nav>			
	</div>
	<div class="tp-contentwrap1">
		<div class="left wt_25 side_1">
			
			<div class="image_block">
			<img src="<?php echo user_info(session_value("id"),"image_url");?>"/>
			</div>
			<div class="menu_list">
				<li><a href="edit_profile.php">Edit Profile</a></li>
				<li><a href="deleteac.php">Delete Account</a></li>				
			</div>
		</div>
		<div class="right wt_75">
			<?php
					$id=$_GET['id'];
					$sql="SELECT  *  FROM  member  where mem_id='$id'";
					$result=mysqli_query($con,$sql);
					$rows=mysqli_fetch_array($result);
			?>
			<div class="tp-contentwrap2">
			<div class="strip-profile"><?php echo $rows['username']."'s";?> Profile</div>
			<table>
			<?php 
				$id=$rows['mem_id'];
			?>
			<tr>
				<th class="td_1" colspan="2">
					<center><?php $image=user_info($id,"image_thumb");?>	
					<a href="" target="_blank"><img src="<?php echo $image;?>" width=""/></a></center>
				</th>
			</tr>
			<tr>
				<td class="td_1">Username :</td>
				<td  class="left"><?php  echo  $rows['username'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Name :</td>
				<td  class="left"><?php  echo  $rows['name'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Email id :</td>
				<td  class="left"><?php  echo  $rows['email'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Date Of Birth :</td>
				<td  class="left"><?php  echo  $rows['dob'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Gender :</td>
				<td  class="left"><?php  echo  $rows['gender'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Profession :</td>
				<td  class="left"><?php  echo  $rows['profession'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Location :</td>
				<td  class="left"><?php  echo  $rows['location'];  ?></td>
			</tr>
			<tr>
				<td class="td_1">Website :</td>
				<td  class="left"><?php  echo  $rows['website'];  ?></td>
			</tr>
			</table>
			<div class="wt_99 left">
			<span  class="td_1">About me</span>
			<span  class="left about"><?php  echo  $rows['about_me'];  ?></span>
			</div>
			</div>
			</div>
		</div>
			
</div>

</div>
</div>
<footer class="left" id="tp-footer">
	<div class="">&copy; Copyright 7topics.com 2015-20.</div>
</footer>
</div>
</div>

</body>
</html>

You can check online working demo of above article using the view demo button below. Also, full source code with all additional css and js file can be downloaded from below:

View Demo                      Download Zip

 

Thank you for reading my article, i hope that you liked my article. For any doubts or any request you can put  your important feedback below in comment box.

Related Tags
Php
Author Of The Post