我收到错误警告:“无法修改标题信息 - 已经发送的标题(在/admin3/public/new_admin.php:7开始输出)在第10行的/admin3/includes/functions.php中”< / p>
它所引用的输出是下面的代码。我不确定它指的是什么部分。如果最后的7表示第7行,那么这与我打开php代码的行匹配。我在这个代码所在的主页的绝对顶部调用了functions.php(错误引用的第二部分)。
<?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php require_once("../includes/validation_functions.php"); ?>
<?php error_reporting(E_ALL); ini_set('display_errors', '1'); ?>
<?php
if (isset($_POST['submit'])) {
$required_fields = array("author", "body");
validate_presences($required_fields);
if (empty($errors)) {
$author = mysql_prep($_POST['author']);
$body = mysql_prep($_POST['body']);
$page_name = ($_POST['page_name']);
$query = "INSERT INTO comments (";
$query .= " author, body, page_name";
$query .= ") VALUES (";
$query .= " '{$author}', '{$body}', '{$page_name}'";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
redirect_to("new_admin.php");
} else {
// Failure
$_SESSION["message"] = "There was an error that prevented the comment from being saved.";
}
}
} else {
$author = "";
$body = "";
}
?>
这是我在上面的代码中使用的函数。 “第10行”代码是“redirect_to”函数:
function mysql_prep($string) {
global $connection;
$escaped_string = mysqli_real_escape_string($connection, $string);
return $escaped_string;
}
function redirect_to($new_location) {
header("Location: " . $new_location);
exit;
}
$ connection是我用于数据库连接的,我已经双重检查了
我不确定如何对此进行故障排除,我之前以类似的方式使用过此功能,因此我不知道是否存在愚蠢的错误或者我没有注意到的事情。谢谢你的帮助!!
答案 0 :(得分:0)
当输出已发送到客户端时,您会收到该错误。确保在发送任何输出之前调用header()
函数...其中包括任何空格,换行或任何print或echo语句。确保在所有HTML和php输出之前调用header()
函数。
如果您的HTML之前有多个PHP块,请确保两者之间没有线路。
如您所见,在您的情况下,在第7行和第5行之前,有一个新行,它作为一些输出提供,这会阻止您修改标题信息。要摆脱这一点,只需删除新行:
<?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php require_once("../includes/validation_functions.php"); ?>
<?php error_reporting(E_ALL); ini_set('display_errors', '1'); ?>
<?php
if (isset($_POST['submit'])) {
$required_fields = array("author", "body");
validate_presences($required_fields);
if (empty($errors)) {
$author = mysql_prep($_POST['author']);
$body = mysql_prep($_POST['body']);
$page_name = ($_POST['page_name']);
$query = "INSERT INTO comments (";
$query .= " author, body, page_name";
$query .= ") VALUES (";
$query .= " '{$author}', '{$body}', '{$page_name}'";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
redirect_to("new_admin.php");
} else {
// Failure
$_SESSION["message"] = "There was an error that prevented the comment from being saved.";
}
}
} else {
$author = "";
$body = "";
}
?>
这应该有效......将来,如果你要重定向,只要确保php块之间没有空格......