Warning: Cannot add header information

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pistolfire99
Forum Commoner
Posts: 58
Joined: Thu May 23, 2002 3:18 pm

Warning: Cannot add header information

Post by pistolfire99 »

Hey guys/gals,
Im developing some sites using php 4.2 and dreamweaver MX with a mysql database. Now here's the deal. There is a form where a user can submit some information and then on pressing the 'submit' button, the data goes into the database. But on the other page I get this weird error on the top.
Warning: Cannot add header information - headers already sent by (output started at C:\Xitami\webpages\quotes\insert_quotes.php:3) in C:\Xitami\webpages\quotes\insert_quotes.php on line 66
If you guys would like a working example of the same, please refer to the url below and submit some information and on doing so, hit the submit button. http://www.i2l2.com/quotes/insert_quotes.php

On doing so, you will see the error on the top that I am refering about.
Here is the section of the code in question:-

Code: Select all

<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
&#123;
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) &#123;
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  &#125;
  return $theValue;
&#125;

$editFormAction = $HTTP_SERVER_VARS&#1111;'PHP_SELF'];
if (isset($HTTP_SERVER_VARS&#1111;'QUERY_STRING'])) &#123;
  $editFormAction .= "?" . $HTTP_SERVER_VARS&#1111;'QUERY_STRING'];
&#125;

if ((isset($HTTP_POST_VARS&#1111;"MM_insert"])) && ($HTTP_POST_VARS&#1111;"MM_insert"] == "form1")) &#123;
  $insertSQL = sprintf("INSERT INTO tblquotes (quotes_subby, quotes_date, quotes_quote, quotes_author) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($HTTP_POST_VARS&#1111;'quotes_subby'], "text"),
                       GetSQLValueString($HTTP_POST_VARS&#1111;'quotes_date'], "text"),
                       GetSQLValueString($HTTP_POST_VARS&#1111;'quotes_quote'], "text"),
                       GetSQLValueString($HTTP_POST_VARS&#1111;'quotes_author'], "text"));

  mysql_select_db($database_connQuotes, $connQuotes);
  $Result1 = mysql_query($insertSQL, $connQuotes) or die(mysql_error());

  $insertGoTo = "view_quotes.php";
  if (isset($HTTP_SERVER_VARS&#1111;'QUERY_STRING'])) &#123;
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $HTTP_SERVER_VARS&#1111;'QUERY_STRING'];
  &#125;
  header(sprintf("Location: %s", $insertGoTo));
&#125;
$date = gmDate("l F d Y");
?>
And this is line 66

Code: Select all

header(sprintf("Location: %s", $insertGoTo));

Please give me some hints and pointers to begin debugging this error.

Thank You.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

when using header() not even a single character of the html-content must be sent to the browser before you send the headers.
  • <?php print('whatever'); header();
  • <html><?php header(...
  • similar
won't work.
You may set output_buffering=<number of bytes> in your php.ini but rather do not. Check if there is content sent before headers (even white-spaces outside <?php ... ?>)
Post Reply