1

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);
?>

4
  • add exit(); after header . example - header("location: google.com"); exit; Commented Oct 1, 2018 at 4:26
  • Please read about SQL injections, PDO and bind parameters and proper data validation as your code above is exposed to all kinds of attack's and to answer your question your redirect is not happening because your INSERT FAILED Commented Oct 1, 2018 at 4:37
  • @BobbyAxe I can insert data on my database with that code above. Thanks Commented Oct 1, 2018 at 4:50
  • If so use ob_start(); at the very top of your script Commented Oct 1, 2018 at 4:56

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

exit is important because the redirect does not end the execution of the current code "scope" so in your code it may still do mysqli_close($link); even though you called header before it. In many cases this can produce undesirable results. So +1 for including that and the warning about output. PHP will close the connection at the end of execution, so in most cases you don't need to call mysqli_close anyway.
One time I saw a header redirect inside of a while loop with no exit, it did all kinds of weird things ... lol. That was some old PHP4 code back in 2009 ish ... lol. .. it was pretty insane. You can use output before a header call if you use output buffering ob_start and the other ob_* functions. But it can be hard to keep track of. It (output buffering) works wonderful with AJAX stuff too.
@Mahbubul Islam I've try still not redirecting to another page
Welcome :) @Red You can accept and up vote my answer if you like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.