Hi, its me again!
I have faced this error trying to redirect from one page.
Here is the error:

Warning: Cannot modify header information - headers already sent by (output started at I:\xampplite\htdocs\site\insert.php:3) in I:\xampplite\htdocs\site\insert.php on line 28
Illegal action or other errors!

here is insert.php

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<?php 
//$_SESSION["name"] = "Test Admin";
$content = $_POST["content"];
//prepare data
//array should have description , heading , contents, creator 
//$array_values function name insertdata
$values['description'] = $_POST["content"];
$values['heading'] = $_POST["heading"];
$values['contents'] = $_POST["contents"];
//this will include currently loggen in user
$values['creator'] = "Test Admin" ;//$_SESSION["name"];

//database call
require("/includes/inc.database.php");
$db = new Connectdb();
$conn = $db->connect();
$data = $db->retrievedata($conn, 'articles');

if ($db->insertdata($conn, $values)){
	echo("Insert Succesful!");
	header('Location:/site/view.php');
	die("Illegal action or other errors!");
	}
else{
	echo("Failed to write an article");
	header('Location:/site/editor.php');
	die("Illegal action or other errors!");
}

?>
</body>
</html>

Recommended Answers

All 4 Replies

You get this error, because you've already outputted the doctype on the first line. You can remove it, because your code will always redirect. Same goes for the /body and /html tag. Also the echo's before the header cause this problem.

so in such a code as mine how do I do redirect after operation like inserting tables successful? Any header tutorial?

<?php 
//$_SESSION["name"] = "Test Admin";
$content = $_POST["content"];
//prepare data
//array should have description , heading , contents, creator 
//$array_values function name insertdata
$values['description'] = $_POST["content"];
$values['heading'] = $_POST["heading"];
$values['contents'] = $_POST["contents"];
//this will include currently loggen in user
$values['creator'] = "Test Admin" ;//$_SESSION["name"];
//database call
require("/includes/inc.database.php");
$db = new Connectdb();
$conn = $db->connect();
$data = $db->retrievedata($conn, 'articles');
if ($db->insertdata($conn, $values)){ header('Location:/site/view.php'); }
else { header('Location:/site/editor.php'); }
?>

more than this seems unneccessary,
the user has two distinct results for two possible query states, view the entered data or forced to re-enter it
**edit
the redirect to the editor page should populate the values of this $_post array to the appropriate input fields. so the user can see what else has to be entered/altered else { header("Location:/site/editor.php?content=$_POST['content']&heading=$_POST['heading']&contents=$_POST['contents']"); } ignoring the insecurity of url parameters, its a thought process at this time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.