Jump to content

Header already exist error...


Jim R

Recommended Posts

I have a form set up to enter names into a database. If the name already exists, it should produce a message telling us that. When I submit that doesn't exist, it works just fine. When I test it with a name that meets the criteria I have set up, it produces "This player is already in the database," as it should, but I get the following warning.

 

Warning: Cannot modify header information - headers already sent by (output started at /home/jwrbloom/public_html/resources/playerEntry/dbEnter.php:31) in/home/jwrbloom/public_html/resources/playerEntry/dbEnter.php on line 76 (76 is the last line of code)

 

 

It does follow through and email me too. (I haven't tried to alter that yet.)

 

 



$nameFirst = $_POST['nameFirst'];
$nameLast = $_POST['nameLast'];
$cityHome = $_POST['cityHome'];
$school = $_POST['school'];
$year = $_POST['year'];
$position = $_POST['position'];
$feet = $_POST['feet'];
$inches = $_POST['inches'];
$level = $_POST['level'];
/*
search for existing row
*/
$sql = "SELECT id FROM a_playerRank WHERE nameFirst='".mysql_real_escape_string($nameFirst)."' AND nameLast='".mysql_real_escape_string($nameLast)."' AND school='".mysql_real_escape_string($school)."'";
if(!$result = mysql_query($sql)) {
  die(mysql_error()."<br />Query: ".$sql);   
}
if(mysql_num_rows($result)) {
 $row = mysql_fetch_assoc($result);
 /*
 This tells us we already have him.
 */
{echo 'This player is already in the database.';  
  }
}
else {
 /*
 insert new row
 */   
 $sql = "INSERT INTO a_playerRank SET 
             grouping='4', 
    nameFirst='".mysql_real_escape_string($nameFirst)."',
             nameLast='".mysql_real_escape_string($nameLast)."',
    city='".mysql_real_escape_string($cityHome)."',
    school='".mysql_real_escape_string($school)."', 
    year='".mysql_real_escape_string($year)."',     
             position='".mysql_real_escape_string($position)."', 
             feet='".mysql_real_escape_string($feet)."', 
             inches='".mysql_real_escape_string($inches)."',
    level='".mysql_real_escape_string($level)."'";
  if(!$result = mysql_query($sql)) {
     die(mysql_error()."<br />Query: ".$sql);   
  }
}




// Email to me
$message =  $_POST['nameFirst'] . " " . $_POST['nameLast'] ." was added to the database.\n";
$message .=  $_POST['feet'] . "'" . $_POST['inches'] ."\", " . $_POST['year'] . "; " .  $_POST['school'] ."\n";
$message .= $_POST['position']."\n";
$message .=  $_POST['email'];

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);

$headers = "From: HHR Database Entry <[email protected]>";

// Send
mail('[email protected]', 'Player database addition', $message, $headers);


/*
redirect user
*/
header("Location:http://hoosierhoopsreport.com/dbpe");

Link to comment
https://forums.phpfreaks.com/topic/273279-header-already-exist-error/
Share on other sites

You are generating output here:

 

echo 'This player is already in the database.';

 

And you are attempting to edit the header here:

 

header("Location:http://hoosierhoopsreport.com/dbpe");

 

You cannot edit the header once you have started producing output (provided that you are not using an output buffer).

We try to discourage people from using output_buffering on a page -

 

1) It takes additional memory and processing time.

 

2) If you are redirecting on a page, it will hide both php error messages and any messages your code outputs that would help you determine why your code on that page isn't doing what you expect.

 

3) It doesn't work for one of the things that causes header errors, so suggesting it as a 'fix-all' doesn't always work.

 

Also, by using output_buffering to hide the problems that cause header errors, your code ends up being more disorganized, so that you or the next person who must use that code or debug what it is doing, will have a harder time.

 

It's always better to find and fix the root cause of a problem, then to mask the problem.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.