Red
Red

Reputation: 37

PHP redirect after inserting data

I'm having issues on redirecting to another page after inserting data.

I'm working on localhost i'm trying to redirect into another page (url) after insertin data.

With the code below, I try putting google url it doesn't redirect it stays on the page after submission.

Any help would be appreciated.

Thanks

<?php
/**
 * 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.";
    header("location: http://www.google.com");	
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
mysqli_close($link);
?>

Upvotes: 1

Views: 2903

Answers (1)

Mahbubul Islam
Mahbubul Islam

Reputation: 1018

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.

Upvotes: 2

Related Questions