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

:)

Dani AI

Generated

The error "Cannot modify header information - headers already sent by (output started at delete.php on line 13)" means PHP already sent output (even a single space or a UTF‑8 BOM) before a header() call. Headers are sent as soon as the first byte of output is emitted, so header() must run before any output. correctly noted that visible output (HTML, echo, print, or die) will prevent a redirect; suggested ob_start() as a workaround, and supplied the affected files (delete.php, db.php, view.php), which help narrow the likely causes.

Practical checklist to find the stray output:

  • Look for stray whitespace or blank lines before <?php or after a closing ?> in every included file.
  • Remove the closing ?> from pure-PHP include files (common safe practice).
  • Verify files are saved as "UTF-8 without BOM" — a BOM will count as output.
  • Search for any echo/print/die/trigger_error or accidental HTML sent before the header call (view.php contains a visible success message in the thread).
  • If an include emits an error string (for example via die(mysql_error())), that counts as output.

Fixes and best practices:

  • Stop producing output prior to sending headers; log errors with error_log() or throw exceptions instead of printing them.
  • After issuing a redirect header, immediately halt execution (for example with exit) so later template code cannot send output.
  • Use ob_start() only as a temporary patch while locating the root cause — it hides the symptom.
  • Consider upgrading from deprecated mysql_* functions to mysqli or PDO for improved error handling and future compatibility.

A quick validation step: create a tiny script that only sends a redirect and nothing else to confirm the server accepts headers, then reintroduce includes one at a time until the offending output is found. This will resolve the "headers already sent" warning and make redirects reliable.

Recommended Answers

All 4 Replies

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.

Member Avatar for Member #120589

Obviously - you've echoed 'Saved'.

Re-read my last post.

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

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.