我在完成任务后尝试将config.php
文件包含在数据库中,重定向到其他页面但显示此错误消息。
“警告:无法修改标头信息 - 已发送的标头 (输出始于 /home/prasan/public_html/theyplay.in/texttospeech/config.php:16)in /home/prasan/public_html/theyplay.in/texttospeech/input.php上线 14"
<?php
include("config.php");
try {
$mi = $_POST['main'];
$i = $_POST['item'];
$dbo->query("INSERT INTO LD (mainitem, subitem, item,fav)VALUES ('$mi','$i',0)") ;
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
header("Location: index.php");
?>
修改:config.php
<?php
$dbhost_name = "localhost";
$database = "rrr";
$username = "rrr";
$password = "rrr";
try {
$dbo = new PDO('mysql:host=' . $dbhost_name . ';dbname=' . $database, $username, $password);
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
答案 0 :(得分:0)
标题重定向位置在try里面。因为你在header
函数之前有一个回声
<?php
include("config.php");
try {
$mi = $_POST['main'];
$i = $_POST['item'];
$dbo->query("INSERT INTO LD (mainitem, subitem, item,fav)VALUES ('$mi','$i',0)") ;
header("Location: index.php");
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
答案 1 :(得分:0)
这里,php已经在第16行
config.php 中向网络服务器发送了标头和输出缓冲区你应该在任何回音或打印之前设置所有标题,
尝试从配置文件中删除任何/所有echo语句,否则从input.php中删除header("Location: index.php");
编辑:有一个选项供您使用,请替换为此代码
<?php
include("config.php");
try {
$mi = $_POST['main'];
$i = $_POST['item'];
$dbo->query("INSERT INTO LD (mainitem, subitem, item,fav)VALUES ('$mi','$i',0)") ;
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
echo '<script>document.location = "index.php";</script>';
?>