If you echo or print something before header() it won't redirect. So you need to remove the echo before the header(). It is also very safe to use an exit() after the header().
/**
* Template Name: Booking Template
*/
get_header();
require_once("db.php");
if(count($_POST)>0) {
$sql = "INSERT INTO bookings (reason, doctor, bookingDate, bookingTime, patientName, gender, phone, email) VALUES ('" . $_POST["reason"] . "','" . $_POST["doctor"] . "','" . $_POST["bookingDate"] . "','" . $_POST["bookingTime"] . "','" . $_POST["patientName"] . "','" . $_POST["gender"] . "','" . $_POST["phone"] . "','" . $_POST["email"] . "')";
if(mysqli_query($link, $sql)){
//echo "Records inserted successfully."; remove or comment this line
header("location: http://www.google.com");
exit(); // use exit. It's a good practice
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
mysqli_close($link);
If your query is ok and data is inserting correctly but yet not redirecting and you can use Javascript redirecting.
/**
* Template Name: Booking Template
*/
get_header();
require_once("db.php");
if(count($_POST)>0) {
$sql = "INSERT INTO bookings (reason, doctor, bookingDate, bookingTime, patientName, gender, phone, email) VALUES ('" . $_POST["reason"] . "','" . $_POST["doctor"] . "','" . $_POST["bookingDate"] . "','" . $_POST["bookingTime"] . "','" . $_POST["patientName"] . "','" . $_POST["gender"] . "','" . $_POST["phone"] . "','" . $_POST["email"] . "')";
if(mysqli_query($link, $sql)){
//echo "Records inserted successfully."; remove or comment this line
header("location: http://www.google.com");
echo "<script>window.location = 'http://www.google.com';</script>";
exit(); // use exit. It's a good practice
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
mysqli_close($link);
Make sure there is no error in get_header() function and double check for any possible error in the code. Use this second method only if you need solve it just for now.
ob_start();at the very top of your script