954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cannot modify header information - headers already sent by

i dont know why i am having an error in header

Warning: Cannot modify header information - headers already sent by (output started at delete.php on line 13

delete.php

<?php


  include("db.php");  

	$id =$_REQUEST['BookID'];
	
	
	// sending query
	mysql_query("DELETE FROM books WHERE BookID = '$id'")
	or die(mysql_error());  	
	
	header("Location: index.php");

?>


db.php

<?php  
	$conn = mysql_connect('localhost', 'root', '');
	 if (!$conn)
    {
	 die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("Books", $conn);
?>

:)

ceteris
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

If you have any output e.g. html or the die statement (connection or query fail), header() won't work. Try looking up the php.net manual:

http://uk3.php.net/manual/en/function.header.php

It states this in the first few lines.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

i have error in line 28

view.php

<?php
require("db.php");
$id =$_REQUEST['BookID'];

$result = mysql_query("SELECT * FROM books WHERE BookID  = '$id'");
$test = mysql_fetch_array($result);
if (!$result)
		{
		die("Error: Data not found..");
		}
				$Title=$test['Title'] ;
				$Author= $test['Author'] ;					
				$PublisherName=$test['PublisherName'] ;
				$CopyrightYear=$test['CopyrightYear'] ;

if(isset($_POST['save']))
{
	$title_save = $_POST['title'];
	$author_save = $_POST['author'];
	$name_save = $_POST['name'];
	$copy_save = $_POST['copy'];

	mysql_query("UPDATE books SET Title ='$title_save', Author ='$author_save',
		 PublisherName ='$name_save',CopyrightYear ='$copy_save' WHERE BookID = '$id'")
				or die(mysql_error()); 
	echo "Saved!";

	header("Location: index.php");
}
mysql_close($conn);?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form method="post">
<table>
	<tr>
		<td>Title:</td>
		<td><input type="text" name="title" value="<?php echo $Title ?>"/></td>
	</tr>
	<tr>
		<td>Author</td>
		<td><input type="text" name="author" value="<?php echo $Author ?>"/></td>
	</tr>
	<tr>
		<td>Publisher Name</td>
		<td><input type="text" name="name" value="<?php echo $PublisherName ?>"/></td>
	</tr>
	<tr>
		<td>Copyright Year</td>
		<td><input type="text" name="copy" value="<?php echo $CopyrightYear ?>"/></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td><input type="submit" name="save" value="save" /></td>
	</tr>
</table>

</body>
</html>

i have an error in line 28.

ceteris
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Obviously - you've echoed 'Saved'.

Re-read my last post.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

use ob_start(); after the <?php at the very first php code and ob_flush(); before the the end tag ?>

anthonyjpv
Junior Poster in Training
97 posts since Oct 2010
Reputation Points: 16
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You