成功查询后重定向到主页[重复]

This question already has an answer here:

I have a simple form to insert a user to my database, but I want the form to redirect to my homepage once the query is completed. I have tried several ways an either I get an error, the query is not run or it does not redirect. Here is the page for the form as my last trial

<?php
require("db.php");
?>
<?php include 'query.php';?>
<html>
   <head>
      <title>Insert a Record in MySQL Database</title>
   </head>

   <body>
      <?php
         if(isset($_POST['insert']))
         {

            $user_added = $_POST['user_added']; 

            $insert_query = "INSERT INTO userList ";
            $insert_query .= "(Book, User_0, User_1) ";
            $insert_query .= "VALUES ";
            $insert_query .= "('$user_added', '0', '1')";

            $retval = mysqli_query($connection, $insert_query);

            if(!$retval )
            {
                 die ("The error is: " . mysqli_error($connection));
            }
            echo "Updated data successfully
";
         }
         else
         {
            ?>
               <form method="post" action="<?php $_PHP_SELF ?>">
                  <table width="400" border="0" cellspacing="1" cellpadding="2">

                     <tr>
                        <td width="100">Book to Add</td>
                        <td><input name="user_added" type="text" id="user_added"></td>
                     </tr>
                     <tr>
                        <td width="100"> </td>
                        <td> </td>
                     </tr>

                     <tr>
                        <td width="100"> </td>
                        <td>
                           <input name="insert" type="submit" id="insert" value="Insert" onclick="window.location='home.php';">
                        </td>
                     </tr>

                  </table>
               </form>
            <?php
         }
      ?>
   </body>
</html>
<?php
    mysqli_close($connection);
?>

When I tried it this way, it shows the Updated data successfully text but it does not redirect. I tried to make the following approach

if(!$retval )
{
     die ("The error is: " . mysqli_error($connection));
}
else
{
header("Location: test.php");
echo "Updated data successfully
";
}

but I get the following error

Warning: Cannot modify header information - headers already sent by (output started at /home/myusername/public_html/home.php:11) in /home/myusername/public_html/home.php on line 30
Updated data successfully

So the database is updated but the redirect does not work. Of course when I tried the second approach I removed the onclick="window.location='home.php';" from the form. Any suggestions?

</div>

Redirects via PHP are effected by headers. Headers, as the error denotes, cannot be sent once page output has started.

From the PHP manual:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

You have HTML before your PHP is run. (This is generally not a great idea; ideally database and other preparatory PHP work would be done in a separate file, before any output, so that they can influence redirects and other headers.)

One quick way to make your code work would be to fall back to JavaScript, which is happy to redirect at any time.

if( !$retval)
    die ("The error is: " . mysqli_error($connection));
else
    echo "<script>location.href = 'somewhere.php';</script>";

Just before your header("Location: test.php"); add the following so it looks like:

flush();
ob_flush();
header("Location: test.php");

You need to flush the headers in order to set it again.

change

header("Location: test.php");

to

echo "<html><script> window.location.href="test.php";</script></html>";

you have to place header("Location: test.php"); before any echo instruction or html output , so at the begin make your test

use

<?php
  ob_start();
?>

at the top of page.. hope it help you.. for further detail visit this link What's the use of ob_start() in php?