Yes because you used the code ahref
tag and this is how it is supposed to work.If you dont want it to reload try using AJAX.This is not the complete answer but you need to make that work through ajax only.You will find a lot of videos and demos regarding it.
Check this Example:
http://www.w3schools.com/ajax/ajax_php.asp
Update:
Mainfile.php
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<?php
session_start();
$userid=$_SESSION['userid'];
?>
<div id="like">
<a href="#" id="postid" onclick="likeclick(this);">Like</a>
<!-- here id should be different for every post .I would prefer using post id to this ahref id because i would use that to detect what post it is actually. -->
</div>
</html>
<script type="text/javascript">
function likeclick(element)
{
var postid=element.id;
var userid=<?php echo json_encode($userid);?>;
//You can use different methods to pass variable to javascript.I used this one because it is easy to implement
//it has some cons to it .Do check for that on google also.
//http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript
//Check this link for more.
$.ajax({
type:'POST',
url:'getlike.php',
data:{"userid":userid,"postid":postid},
success : function(content)
{
$('#like').html(content);
//This gets the html content from the getlike page and displays in the div on this page.
//Note:I have used .html which replaces any previous content inside the 'like' div.
})
}
</script>
getlike.php
<?php
require 'connect.inc.php'; //This make a connection to the database
$userid=$_POST['userid'];
$postid=$_POST['postid'];
$statement=$mysqli->prepare("select `likes` from `posts` where `postid`=?");
$statement->bind_param("s",$postid);
$statement->execute();
$result=$statement->get_result();
while($row=$result->fetch_assoc())
{
$likes_on_this_post=$row['likes'];
}
$likes_on_this_post=$likes_on_this_post+1; //Added one like more.
$statement=$mysqli->prepare("update `posts` set `likes`=?");
$statement->bind_param("s",$likes_on_this_post);
$statement->execute();
echo "+".$likes_on_this_post;
//This echo is actually the main thing.When this page runs through ajax the response is given back to the calling object
//What goes with response are the html contents on this page as well as whatever i echo on this page.
//Considering this example i have only echoed the no of likes and it doesn't contain any sort of html content so only the echoed element goes.
?>