0

I am stuck with my php code. Parent php page reload again after header() works, Here is my code

<?php
$mysqli = new mysqli("localhost", "root", "root", "testing");
 /* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$sql ="update test set hit_count = hit_count+1";

$result = $mysqli->query($sql);

header('Location: http://www.google.com/');
die();
?>

Here some times i got 2 hit_count from db. How it works, i added die() after header().

Clay
  • 4,700
  • 3
  • 33
  • 49
  • Possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Akshay Khetrapal Oct 20 '16 at 05:41

1 Answers1

0

You should not output text before using header redirects as seen on the code below:

/* Select queries return a resultset */
if ($result = $mysqli->query($sql)) {
    printf("updated");
}else{
    echo "failed";
}
rai
  • 449
  • 3
  • 10