1

I have wrote a script to update a MySql DB from a form.

After the DB has been updated I want the page to auto redirect to another page.

This has been working fine however since switching hosting provider non of my sites re-directs work.

Here is the code:

<?php
$servername = "localhost";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

 $id= $_POST[id];
 $dob=$_POST[dob];

$sql=("update users set dob='$dob' where id='$id'")or die('Error 23 ' . mysql_error());

if ($conn->query($sql) === TRUE) {
    echo "Updated successfully<br /><br />";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
<?php
header("location:index.php?action=updated");  ?>

When I run the code the DB updates but the page just displays Updated successfully?

7
  • u can move the redirect code, i:e the header function to if part after displaying the message updated successfully Commented Sep 30, 2015 at 17:25
  • is there any .htaccess file in your domain root folder? if so then remove it and then see you header is working or not? Commented Sep 30, 2015 at 17:27
  • Moving the code hasn't made a difference. Commented Sep 30, 2015 at 17:28
  • @PayerAhammed - Nope .htaccess file Commented Sep 30, 2015 at 17:29
  • try header with absolute path like header('Location: http://www.example.com/index.php?action=updated'); Commented Sep 30, 2015 at 17:32

3 Answers 3

2

try using javascript to redirect like below:

if ($conn->query($sql) === TRUE) {
    echo "<script>
          alert('Updated successfully');
          window.location.href = 'index.php?action=updated';
         </script>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, it redirects. Any idea why I cant use PHP to do this anymore?
if u use header function it will directly redirect you without giving the message that it is updated successfully.
0

Don't echo anything and try to redirect afterwards. Instead simply redirect without any echoing.

if ($conn->query($sql) === TRUE) {
    header("location:index.php?action=updated");
    exit;
} else
    echo "Error: " . $sql . "<br>" . $conn->error;

1 Comment

It still isn't redirecting?
0

try this :

<?php 
ob_start();
header('Location: http://www.example.com/index.php?action=updated', true); 
?>

2 Comments

Nope does not re-direct?
use <?php ob_start(); ?> this before header

Your Answer

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