Hi guyz i am getting an error on my header information..the warnig is displayed
when this part of code Runs

echo '<a href="' . 'ecomm_catalogue_view.php?product_code=' . $product_code .'" >'; '</a>';
            header("location: ecomm_catalogue_view.php?' . 'product_code=' . $product_code . '");

the warning displayed on top of each page is:

Warning: Cannot modify header information - headers already sent by (output started at \romajir\search.php:19) in C:\xampp\htdocs\romajir\search.php  on line 20

i dont know how to solve this any suggestion would help..thanx in advance
the search code for search.php is

<?php
include 'db.inc.php';
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));

$search= (isset($_POST['search'])) ? trim($_POST['search']) : '';
if (isset($_POST['submit']) && $_POST['submit'] == 'Go') {
    if (empty($search)) {
    echo 'Their is Nothing to search!!';
    }
    else {
        $query = 'SELECT product_code FROM ecomm_products WHERE name = "' .
        $search . '"';
        $result = mysql_query($query, $db) or die(mysql_error());
        if (mysql_num_rows($result) > 0){
            $row = mysql_fetch_assoc($result);
            $product_code=$row['product_code'];
            echo '<a href="' . 'ecomm_catalogue_view.php?product_code=' . $product_code .'" >'; '</a>';
            header("location: ecomm_catalogue_view.php?' . 'product_code=' . $product_code . '");
            }
        else{
            echo 'We don\'t have that product. Sorry...';
            $search = '';
        }
    }
}
?>

Dani AI

Generated

Short answer: the PHP warning means some output reached the browser before your redirect was sent. In this thread correctly pointed out a quoting/concatenation bug and reminded you about stray whitespace — both are common causes. The warning’s file:line points to where output started, but invisible output (UTF‑8 BOM, editors adding a newline, or an included file that echoes) is the usual culprit.

Checklist to fix and verify

  • Remove any characters before the opening <?php and avoid a closing ?> on files that contain only PHP — that prevents accidental trailing whitespace.
  • Inspect every included file for output (echo/print/HTML) and for a UTF‑8 BOM; many editors silently add a BOM which breaks headers. Use a hex editor or this quick detection method to confirm a BOM:
    $contents = file_get_contents(__DIR__.'/db.inc.php');
    if (substr($contents, 0, 3) === "\xEF\xBB\xBF") {
      // BOM present — re-save file as UTF-8 without BOM
    }
  • If you must allow early output, use output buffering as a fallback (see ob_start), but buffering is a workaround, not a substitute for clean files.

Header usage and good practice

  • Use the standard header format and stop execution immediately after sending a redirect so no further output runs. Also URL‑encode any query values to avoid illegal characters.
  • Replace deprecated mysql_* code with mysqli or PDO and use prepared statements for user input — the search/query code in the thread is vulnerable if it uses raw POST data.

References: PHP manual for header() (https://www.php.net/manual/en/function.header.php), output buffering (https://www.php.net/manual/en/function.ob-start.php), and guidance on choosing MySQL APIs (https://www.php.net/manual/en/mysqlinfo.api.choosing.php).

Recommended Answers

All 8 Replies

You can send header after soemthing is ECHOed. Place the header statements above all echo statements and also make sure that there arent any blank lines or spaces.

header statements can be placed after echo statements.
check there isn't even a space before first <?PHP
also try

include('db.inc.php');

brackets are important.

@shubhamjain, once i place it on top of the echo still it doesnt work..& where should i remove the space...i can't see any spaces within my code

i have done as u hve told me but now it doesnt move to the other page!!

header("location: ecomm_catalogue_view.php?' . 'product_code=' . $product_code . '");

you are mixing quotes
try

header("location: ecomm_catalogue_view.php?product_code=$product_code");
//or
header('location: ecomm_catalogue_view.php?product_code='.$product_code);

i think this is your problem

the general rule with quotes is that using "" doublequotes php will search through the string for things like $variable or $_SESSION or $var{$variable2}iable etc.
where as using ' ' single quotes they must be appended.
echo 'this is a '.$variable.' and a session'.$_SESSION;
you also need to close a text node you have opened.
like

'. . . product_code=' . $product_code . '");

after $product_code you opened another text node but closed with double quotes.
it is better to end without any quotes or $var.''
hope this helps :D

thanx i avoided using the code for ECHOing before the header and replaced tha header quotes with single quotes and it worked...thanx alot

cheerz!!!

No problems always glad to help :)

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.