3

I am trying to include config.php file for database after completing task , redirect to other page but it shows this error message.

"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");
?>  

Edit: 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();
}
?>
Thorsten
  • 5,634
  • 6
  • 35
  • 33
Lalit Singh
  • 329
  • 1
  • 2
  • 9

2 Answers2

0

Header redirection place inside try. because you have an echo before header function

<?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";
}

?>  
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
0

here, php has already sent headers and output buffer to webserver in config.php on line 16

you should set all headers before any echo or print,

try removing any/all echo statements from config file, otherwise remove header("Location: index.php"); from you input.php

EDIT: one option is there for you, replace by this code

<?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>';
?> 
  • but i want to redirect my page to other location,any other way to redirect to other php page, if it is possible plz tell me actually am new in php so i need help. – Lalit Singh Jun 01 '14 at 10:39
  • you can use javascript redirect `window.location` or `document.location` one more possible way is using **META** tag `` if you are redirecting then what is the use of `"Error!: " . $e->getMessage() . "
    ";` as user will not see this
    – Manjeet Sharma Jun 01 '14 at 10:45