Solved

Headers already sent issue, Can i just remove the goto lines of code?

Posted on 2013-01-18
6
163 Views
Last Modified: 2013-01-21
I have a form that is deleting a user.  It works fine, but gives me the "Headers already sent" error on the page.  I'd like to not have that show up and get this to work correctly.

I have attached the snippet that I am using after the "DELETE" from DB code.

Open in new window

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "infoID")) {
if ((isset($_GET['userID'])) && ($_GET['userID'] != "")) {
  $deleteSQL = sprintf("DELETE FROM tblusers WHERE userID=%s",
                       GetSQLValueString($_GET['userID'], "int"));

  mysql_select_db($database_con_db_local, $con_db_local);
  $Result1 = mysql_query($deleteSQL, $con_db_local) or die(mysql_error());

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

Open in new window

0
Comment
Question by:axessJosh
6 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 38793584
There must be absolutely nothing sent to the browser before the header function, even a blank line before the <?php tag will cause the error you cannot echo or do anything else that sends something to the browser before the header.

Cd&
0
 
LVL 2

Author Comment

by:axessJosh
ID: 38793593
the file that is being used to process the deletion is inserted using:

<?php include_once (); ?>

since the file its included in is also PHP, could there be a conflict of some kind there?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 38793670
Yes, there could be a conflict there.  There is probably a newline character of invisible whitespace.  You can add ob_start() as the first PHP statement in the first script and it will probably solve the problem.  It may make your scripts run faster, too!
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38795941
Extra php brackets cause white space to be added in between them.  The newlines that Ray mentioned are in between the ?> at the end of the first line and <?php on the second line.

<?php include_once (); ?>
<?php include_once (); ?>

Open in new window

0
 
LVL 11

Expert Comment

by:mcnute
ID: 38796232
ob_start();

Open in new window

is your friend as Ray pointed out.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38802414
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now