Jump to content

Warning: Cannot modify header information - headers already sent


skylineview

Recommended Posts

OK I have tried everything I could find here and about 40 other sites.  I don't understand whats going wrong, I understand the problem, but nothing I can find will do the trick.  I continually get the message "Warning:  Cannot modify header information - headers already sent by (output started at C:\Webserver\edit.php:1) in C:\Webserver\edit.php on line 55".  I'd like the form to redirect back to the index.php page when i click on the update button.  So it will update my DB and redirect at the same time. If there is a way for me to take this code and turn it into a "success" type page, that would be perfect too.

I've used Dreamweaver CS4 to create this code automatically, using the wizards ( know lame thing to do) But I'm still trying to learn and understand PHP. 

 

Can someone please help me find the problem with this code and help me correct it.  I've tried moving the header() to the top I've tried the ob_start() tags, I've tried removing the header() completely and I've triple verified there is no white space.

 

I appreciate your help

 

 

<?php virtual('/Connections/bday.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    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;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE birthday SET firstname=%s, lastname=%s, dob=%s WHERE id=%s",
                       GetSQLValueString($_POST['firstname'], "text"),
                       GetSQLValueString($_POST['lastname'], "text"),
                       GetSQLValueString($_POST['dob'], "text"),
                       GetSQLValueString($_POST['id'], "int"));

  @mysql_select_db($database_bday, $bday);
  $Result1 = mysql_query($updateSQL, $bday) or die(mysql_error());

  $updateGoTo = "/index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

$colname_rs1 = "-1";
if (isset($_GET['id'])) {
  $colname_rs1 = $_GET['id'];
}
mysql_select_db($database_bday, $bday);
$query_rs1 = sprintf("SELECT * FROM birthday WHERE id = %s", GetSQLValueString($colname_rs1, "int"));
$rs1 = mysql_query($query_rs1, $bday) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
$totalRows_rs1 = mysql_num_rows($rs1);
?>
<html>
<body>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">Firstname:</td>
      <td><input type="text" name="firstname" value="<?php echo htmlentities($row_rs1['firstname'], ENT_COMPAT, ''); ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Lastname:</td>
      <td><input type="text" name="lastname" value="<?php echo htmlentities($row_rs1['lastname'], ENT_COMPAT, ''); ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Dob:</td>
      <td><input type="text" name="dob" value="<?php echo htmlentities($row_rs1['dob'], ENT_COMPAT, ''); ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"> </td>
      <td><input type="submit" value="Update record"></td>
    </tr>
  </table>
  <input type="hidden" name="id" value="<?php echo $row_rs1['id']; ?>">
  <input type="hidden" name="MM_update" value="form1">
  <input type="hidden" name="id" value="<?php echo $row_rs1['id']; ?>">
</form>
<p> </p>
</body>
</html>
<?php
mysql_free_result($rs1);
?>

Link to comment
Share on other sites

First of all, read the error message -

 

output started at C:\Webserver\edit.php:1

 

Line 1 of edit.php is causing output to be sent. Anything you do after line 1 in that file cannot possibly fix the problem.

 

You either have some non-printing character before the <?php tag (like a new-line) or your file has been saved as a UTF-8 format file an the BOM (Byte Order Mark) characters that the editor puts at the start of the file are the output that is being sent.

Link to comment
Share on other sites

I do not know why you are going in and out of php for an include, but try this:

 

Change:

<?php virtual('/Connections/bday.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {

 

To:

<?php 
virtual('/Connections/bday.php'); 
if (!function_exists("GetSQLValueString")) {

 

And see if that gets rid of the header error.

Link to comment
Share on other sites

First of all, read the error message -

 

output started at C:\Webserver\edit.php:1

 

Line 1 of edit.php is causing output to be sent. Anything you do after line 1 in that file cannot possibly fix the problem.

 

You either have some non-printing character before the <?php tag (like a new-line) or your file has been saved as a UTF-8 format file an the BOM (Byte Order Mark) characters that the editor puts at the start of the file are the output that is being sent.

 

Ok, I am a nuuuub, I had it saving under the "All Documents" type in dreamweaver.  Re-saved it as a PHP file, and that did the trick.  I didn't know that would make a difference  Thanks for you help!

Link to comment
Share on other sites

First of all, read the error message -

 

output started at C:\Webserver\edit.php:1

 

Line 1 of edit.php is causing output to be sent. Anything you do after line 1 in that file cannot possibly fix the problem.

 

You either have some non-printing character before the <?php tag (like a new-line) or your file has been saved as a UTF-8 format file an the BOM (Byte Order Mark) characters that the editor puts at the start of the file are the output that is being sent.

 

Well I went to create an insert records page, and its doing the same thing.  Just like the update records page, I want it to go back to index.php.  I am geting the same stupid error again. 

 

Here is my code:

<?php virtual('/Connections/edit.php');
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    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;
  }
  return $theValue;
}
}
mysql_select_db($database_edit, $edit);
$query_rs1 = "SELECT * FROM birthday";
$rs1 = mysql_query($query_rs1, $edit) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
$totalRows_rs1 = mysql_num_rows($rs1);
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO birthday (id, firstname, lastname, dob) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['firstname'], "text"),
                       GetSQLValueString($_POST['lastname'], "text"),
                       GetSQLValueString($_POST['dob'], "text"));

  mysql_select_db($database_edit, $edit);
  $Result1 = mysql_query($insertSQL, $edit) or die(mysql_error());

  $insertGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">Firstname:</td>
      <td><input type="text" name="firstname" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Lastname:</td>
      <td><input type="text" name="lastname" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Dob:</td>
      <td><input type="text" name="dob" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"> </td>
      <td><input type="submit" value="Insert record"></td>
    </tr>
  </table>
  <input type="hidden" name="id" value="">
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p>
</body>
</html>
<?php
mysql_free_result($rs1);
?>

Link to comment
Share on other sites

I just don't understand whats going on.  There is no whitespace or characters before or after any of my tags.  The edit.php page worked GREAT when I resaved the file as a .php type (was .php but not .php type) Now i've created this insert.php file and its giving me the same error, I've tried re-saving, taking out the extra <?php and ?> tags,  I don't know what else is wrong.  Please help.

 

Thanks.

Link to comment
Share on other sites

What is the current error message, because the output could be coming from a different place than for the previous error.

 

In general you must save files as ANSI (ASCII) or if you must save them as UTF-8, save it without the BOM (most programming editors have a setting to do it this way.)

Link to comment
Share on other sites

What is the current error message, because the output could be coming from a different place than for the previous error.

 

In general you must save files as ANSI (ASCII) or if you must save them as UTF-8, save it without the BOM (most programming editors have a setting to do it this way.)

 

Its the same error as my first post, just a different file doing something slightly different.  Instead of editing a record I'm trying to insert a new one, then revert back to the index.php page.  I've done this all in Dreamweaver, let me see if I can change the way the page is saved.

Link to comment
Share on other sites

What is the current error message, because the output could be coming from a different place than for the previous error.

 

In general you must save files as ANSI (ASCII) or if you must save them as UTF-8, save it without the BOM (most programming editors have a setting to do it this way.)

 

This is the Error (basically the same as my first posting)

 

Warning: Cannot modify header information - headers already sent by (output started at C:\Webserver\insert.php:1) in C:\Webserver\insert.php on line 59

 

From what I can understand its basically the same code, just doing something different when the button is pushed.  I don't understand why I got the first page working, but I create a new page and it doesn't work.

Link to comment
Share on other sites

If the following is line 1 of insert.php, the problem could be in the edit.php file -

<?php virtual('/Connections/edit.php');

 

A) Put the first opening <?php tag on its own line to uniquely identify where the output is happening.

B) Make sure that edit.php has no white-space before the first opening <?php tag or after the last closing ?> tag and that it has been saved in ANSI format.

Link to comment
Share on other sites

If the following is line 1 of insert.php, the problem could be in the edit.php file -

<?php virtual('/Connections/edit.php');

 

A) Put the first opening <?php tag on its own line to uniquely identify where the output is happening.

B) Make sure that edit.php has no white-space before the first opening <?php tag or after the last closing ?> tag and that it has been saved in ANSI format.

 

I can only assume it was the same issues, I just recreated the file and resaved, deleted everything from the server, and now it seems to be working correctly.  IDK what gives, maybe its me just making stupid n00b mistakes.

 

Thanks for all your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.