【问题标题】:Redirect to homepage after successful query [duplicate]查询成功后重定向到主页[重复]
【发布时间】:2015-12-04 08:47:59
【问题描述】:

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

<?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';"。有什么建议吗?

【问题讨论】:

    标签: php mysql redirect sql-insert


    【解决方案1】:

    使用

    <?php
      ob_start();
    ?>
    

    在页面顶部..希望对您有所帮助.. 有关更多详细信息,请访问此链接 What's the use of ob_start() in php?

    【讨论】:

      【解决方案2】:

      你必须放置 header("Location: test.php");在任何 echo 指令或 html 输出之前,所以在开始时进行测试

      【讨论】:

        【解决方案3】:

        通过 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>";
        

        【讨论】:

        • 谢谢,javascript 重定向就像一个魅力。我仍然会尝试看看如何让这个重定向与 PHP 一起工作,但这是一个进步。
        • 您可能希望查看像 CodeIgniter 这样的 MVC(模型、视图、控制器)库。这些(通常)强制执行 PHP 发生预输出的概念,并附带一整个世界的便利库和帮助程序,为您节省大量代码时间。
        【解决方案4】:

        改变

        header("Location: test.php");
        

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

        【讨论】:

        • 这将使用完全不同的重定向方法和完全不同的行为。
        【解决方案5】:

        就在您的 header("Location: test.php"); 之前添加以下内容,使其看起来像:

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

        您需要刷新标题才能重新设置。

        【讨论】:

        • 当我尝试这个时,我仍然得到同样的错误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
        猜你喜欢
        • 2019-07-11
        • 2015-07-12
        • 2019-09-11
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        • 2022-09-23
        • 1970-01-01
        • 2016-07-15
        相关资源
        最近更新 更多