我试图在完成任务后为数据库包含 config.php
文件,重定向到其他页面,但它显示此错误消息。
"Warning: Cannot modify header information - headers already sent by (output started at /home/prasan/public_html/theyplay.in/texttospeech/config.php:16) in /home/prasan/public_html/theyplay.in/texttospeech/input.php on line 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();
}
?>
最佳答案
header 重定向放在 try 里面。因为你在 header
函数之前有一个 echo
<?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";
}
?>
关于php - include 和 header 函数不能在单个 php 文件中一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23978684/