1

I am aware that there should be no whitespace before the php code. However my code gives me the following error on the live server, but not on my test Apache server:

Warning: Cannot modify header information - headers already sent by (output started at /home/mydomain/public_html/parts/includes/dbinfo.inc.php:5) in /home/mydomain/public_html/parts/update_part.php on line 55

I have tried removing the functions.inc.phpstill getting the same problem. Below are the 2 pieces of code involved: a) dbinfo.inc.php

    <?php
$username="myusername";
$password="mypassword";
$database="mydatabase";
?> 

b) update_part.php

<?php

include("./includes/dbinfo.inc.php");
include("./includes/functions.inc.php");

mysql_connect("localhost","$username","$password") or die("Error: ".mysqlerror());
mysql_select_db("$database"); 

//get the variables we transmitted from the form
        $part_id            = $_POST['part_id'];
        $part_manufacturer      = $_POST['part_manufacturer'];
        $part_descr             = $_POST['part_descr'];
        $part_price         = $_POST['part_price'];
        $part_code          = $_POST['part_code'];
        $part_status            = $_POST['part_status'];
        $part_image             = $_POST['part_image'];
        $part_cat           = $_POST['part_cat'];
        $ave_mth_usage          = $_POST['ave_mth_usage'];
        $reorder_level          = $_POST['reorder_level'];
        $reorder_qty            = $_POST['reorder_qty'];
        $other_item_details             = $_POST['other_item_details'];
        $part_stock_taking_date         = $_POST['part_stock_taking_date'];
        $part_qty_in_stock      = $_POST['part_qty_in_stock'];


        $part_manufacturer      = mysql_real_escape_string($part_manufacturer);
        $part_descr             = mysql_real_escape_string($part_descr);
        $part_code          = mysql_real_escape_string($part_code);
        $part_status            = mysql_real_escape_string($part_status);
        $part_cat           = mysql_real_escape_string($part_cat);
        $other_item_details             = mysql_real_escape_string($other_item_details);


//replace TestTable with the name of your table
$sql = 
    "UPDATE part SET 
        part_manufacturer       = '$part_manufacturer',
        part_descr          = '$part_descr',
        part_price          = '$part_price',
        part_code           = '$part_code',
        part_status             = '$part_status',
        part_image          = '$part_image',
        part_cat            = '$part_cat',
        ave_mth_usage           = '$ave_mth_usage',
        reorder_level           = '$reorder_level',
        reorder_qty             = '$reorder_qty',
        other_item_details      = '$other_item_details',
        part_stock_taking_date          = '$part_stock_taking_date',
        part_qty_in_stock       = '$part_qty_in_stock'


    WHERE part_id = $part_id";
//mysql_query($sql) or die ("Error: ".mysql_error());
do_query($sql,__LINE__);
header("Location: ./view_parts.php");
?>

The offending code at line 55 is the line just before the final line of b) i.e.

header("Location: ./view_parts.php");

Any ideas?

potashin
  • 44,205
  • 11
  • 83
  • 107
Geoff
  • 197
  • 2
  • 13
  • Seems your `dbinfo.inc.php` has whitespace before the ` – Niloct Apr 06 '14 at 17:22
  • http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770075#12770075 . Remove the ending `?>` tags. Also, if you're transferring files to the live server via FTP, make sure to use Binary mode rather than Text. – DCoder Apr 06 '14 at 17:22
  • @niloct sorry, that's just a result of copying in to query. It isn't actually there – Geoff Apr 06 '14 at 17:27
  • @Dcoder I didn't realize that ?> tags were not necessary! Since when and why? – Geoff Apr 06 '14 at 17:28
  • dont output anything before using header() in your script, may be you have some output in dbinfo.inc.php, make it sure – Dinesh Apr 06 '14 at 17:28
  • @Dk-Macadamia the code for dbinfo.inc.php is shown in a) above and is just setting up login variables. – Geoff Apr 06 '14 at 17:32
  • 1
    @Geoff: [The `?>` tag is only necessary if it is followed by something you *want* to output](http://us.php.net/manual/en/language.basic-syntax.instruction-separation.php). In practice, using this tag at the end of a file is just asking for trouble - your question is a perfect example of what happens when whitespace gets added to the end of your file either by your IDE (e.g. a setting like "add blank lines to the end of source files"), your FTP client transferring in Text mode and messing up newlines, or other factors. – DCoder Apr 06 '14 at 17:36

4 Answers4

2

Check file encoding - use UTF-8 without BOM. You can use output buffering.

vanion
  • 126
  • 1
  • 8
2

You should delete php closing tag from your php script and all spaces before opening tag and all this again in the included php scripts

potashin
  • 44,205
  • 11
  • 83
  • 107
  • That worked. Thanks also to @Dcoder for mentioning it, and to all you guys with helpful suggestions. – Geoff Apr 06 '14 at 17:38
  • still I would like to know why the closing tags are now unecessary, as I have always used them. Ok I looked it up at this [link](http://stackoverflow.com/questions/3219383/why-do-some-scripts-omit-the-closing-php-tag) – Geoff Apr 06 '14 at 17:40
  • http://stackoverflow.com/q/4410704/3444240 – potashin Apr 06 '14 at 17:41
0

Sounds like you are having an error or warning output to the page. Check the source of the page.

For the 'header' command to work, there must be no prior output.

Output buffering may help if you use ob_clean() . You can also turn off displaying errors in your php.ini file.

Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
  • As a note to the ACCEPTED solution above, the closing tags have never been necessary, in fact, you don't even need to use ` – Kraang Prime Apr 08 '14 at 02:31
0

This might cause an error: remove the spaces before any

<?php 

opening tags.

Maarten van Middelaar
  • 1,691
  • 10
  • 15