【问题标题】:Redirect to landing page: header("Location:index.php");重定向到登陆页面:header("Location:index.php");
【发布时间】:2018-05-28 17:25:28
【问题描述】:

嗨,伙计们试图找到错误页面未更改为 index.php。

不重定向到着陆页一切正常。

从数据库中删除记录,删除是/否的页面也可以

    <?php
    // Process delete operation after confirmation
    if(isset($_POST["ID"]) && !empty($_POST["ID"])){
        // Include config file
        require_once 'config.php';

        // Prepare a select statement
        $sql = "DELETE FROM cv WHERE ID = ?";

        if($stmt = mysqli_prepare($link,$sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "i", $param_ID);

            // Set parameters
            $param_ID = trim($_POST["ID"]);

            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Records deleted successfully. Redirect to landing page

                header('Location:index.php');
               exit;


            }else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);

        // Close connection
        mysqli_close($link);
    } else{
        // Check existence of ID parameter
        if(empty(trim($_GET["ID"]))){
            // URL doesn't contain ID parameter. Redirect to error page
            header("location: error.php");
            exit();
        }

    }
   ?>

任何帮助都会很棒。

警告:无法修改标头信息 - 第 26 行 /storage/public_html/crudbd/delete.php 中的标头(输出开始于 /storage/public_html/crudbd/config.php:31)

【问题讨论】:

  • 你试过绝对路径吗?
  • @Niels 他做了if($stmt = mysqli_prepare($link,$sql))
  • @Niels 他正在分配而不是比较
  • @Niels 不,没有==if ($something = somefunction())$something = somefunction(); if($something) 是一回事

标签: php


【解决方案1】:

只要您回显 ANYTHING,就会发送 HTTP 标头,因此无法再发送,从而导致您的重定向标头失败。

检查 config.php 中的 echo 语句,关闭 display_errors,error_reporting 为 -1,并在终端中跟踪日志以确保任何错误日志内容不会导致 PHP 提前输出。

更新

您粘贴了错误。 Warning: Cannot modify header information - headers already sent by (output started at /storage/public_html/crudbd/config.php:31) in /storage/public_html/crudbd/delete.php on line 26

所以,正如我所说,我猜测 config.php 中有输出是正确的,但它不是回显。

如果您关闭display_errors,它应该可以工作。通知或警告仍会出现在您的日志中。

找出那条线上发生了什么。例如,如果有一个未定义的变量,您没有首先检查 isset(),这可能会触发日志输出。

【讨论】:

  • 你们为什么投反对票?
  • OP 确实声明删除已成功执行。这是否意味着代码执行实际上到达了对标头的调用? (为了记录,我不是反对者)
  • config.php 中没有回显一切正常,但停留在 update.php 而不是 index.php
  • 太棒了!很高兴我能帮上忙。你能把这个标记为正确答案吗? :-)
猜你喜欢
  • 2015-04-11
  • 2013-01-25
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多