0

the upcount and downcount columns are incrementing on every page refresh and that shouldn't happen it should happen only on button click what should i do? here i have implemented upvote and downvote mechanism

//upvote and downvote

<?php




if(isset($_POST['up']))

{
$string= $_POST['up'];
 $d= substr($string,0,2);
 $t = ltrim($d, '0');




$id1=$id;
$conn1=new mysqli('localhost','root','','forum') or die(mysql_error());
 $stmt1= $conn1->prepare("UPDATE messages SET upcount = (upcount+1) WHERE mid ='".$t."' ");



 $stmt1->execute();

        $stmt1->close();



}

if(isset($_POST['down']))

{
$string1= $_POST['down'];
 $d1= substr($string1,0,2);
 $t1 = ltrim($d1, '0');
 echo $t1;
 $conn1=new mysqli('localhost','root','','forum') or die(mysql_error());
  $queryd=mysqli_query($conn1,"SELECT upcount FROM messages where messages.mid='".$t1."'");
     $row=mysqli_fetch_assoc($queryd);
  $up=$row['upcount'];

  if($up>0)



$id1=$id;
$conn1=new mysqli('localhost','root','','forum') or die(mysql_error());
 if($up>0)
 {
$stmt2= $conn1->prepare("UPDATE messages SET downcount = (downcount+1)  WHERE mid ='".$t1."'");
 }


$stmt2->execute();

        $stmt2->close();

        $conn1->close();

}
Theon
  • 9
  • 4
  • Is the browser asking to re-send "form data" to the server when you're requesting page refresh? – Ruslan Osmanov Apr 29 '16 at 06:56
  • yes upcount is incrementing twice but i want it once even if the page is refreshed.But it is working fine after page refresh. – Theon Apr 29 '16 at 07:01
  • Then it's expected behaviour. "Resend" actually means another POST. See also http://stackoverflow.com/questions/4327236/stop-browsers-asking-to-resend-form-data-on-refresh – Ruslan Osmanov Apr 29 '16 at 07:06
  • @RuslanOsmanov i am not getting any message for page refresh by the browser – Theon Apr 29 '16 at 07:35

1 Answers1

0

Just after the update query, you should redirect to the same page, this will empty the posted data from the http header

header('location:'$_SERVER['PHP_SELF']);