-1

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\n";
         }
         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\n";
}

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?

Tavo
  • 173
  • 1
  • 15

5 Answers5

4

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>";
Mitya
  • 33,629
  • 9
  • 60
  • 107
  • Thank you, the javascript redirect works like a charm. I still will try to look how can I get this redirect to work with PHP but this is progress. – Tavo Sep 08 '15 at 15:47
  • You may wish to look at an MVC (model, view, controller) library like CodeIgniter. These (generally) enforce the notion of PHP happening pre-output, and come with a whole world of convenience libraries and helpers to save you a lot of time on your code. – Mitya Sep 08 '15 at 17:09
0

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.

Jake Bown
  • 411
  • 4
  • 11
  • When I try this I still get the same 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 32` – Tavo Sep 08 '15 at 15:45
0

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

Fky
  • 2,133
  • 1
  • 15
  • 23
0

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?

Community
  • 1
  • 1
Sajjad Khan
  • 345
  • 3
  • 17
-1

change

header("Location: test.php");

to

echo "<html><script> window.location.href="test.php";</script></html>";
Awssam Saidi
  • 435
  • 4
  • 14
  • That would use a completely different method of redirect with a completely different behaviour. – Mifeet Sep 08 '15 at 16:41