成功查询后重定向到主页

时间:2015-09-08 15:27:02

标签: php mysql redirect sql-insert

我有一个简单的表单将用户插入我的数据库,但是我希望在查询完成后将表单重定向到我的主页。我尝试了几种方法,我得到一个错误,查询没有运行或它没有重定向。这是我最后一次试用的表格页面

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

当我以这种方式尝试时,它会显示Updated data successfully文本,但不会重定向。我尝试采用以下方法

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

但是我收到以下错误

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

因此数据库已更新,但重定向不起作用。当然,当我尝试第二种方法时,我从表单中删除了onclick="window.location='home.php';"。有什么建议吗?

5 个答案:

答案 0 :(得分:4)

通过PHP重定向受标头影响。一旦页面输出开始,就不能发送标题,如错误所示。

来自PHP manual

  

请记住,在任何实际输出之前必须调用header()   通过普通HTML标记,文件中的空行或PHP发送。

在运行PHP之前,您已拥有HTML。 (这通常不是一个好主意;理想情况下,数据库和其他预备PHP工作将在任何输出之前在单独的文件中完成,以便它们可以影响重定向和其他标头。)

让代码工作的一种快捷方法是回归JavaScript,它很乐意随时重定向。

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

答案 1 :(得分:0)

header("Location: test.php");添加以下内容之前,它看起来像是:

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

您需要刷新标题才能再次设置它。

答案 2 :(得分:0)

你必须放置标题(&#34;位置:test.php&#34;);在任何echo指令或html输出之前,所以在开始时进行测试

答案 3 :(得分:0)

使用

<?php
  ob_start();
?>

在页面顶部..希望它能帮到你.. 有关详细信息,请访问此链接 What's the use of ob_start() in php?

答案 4 :(得分:-1)

更改

header("Location: test.php");

echo "<html><script> window.location.href="test.php";</script></html>";
相关问题