Jim R Posted January 17, 2013 Share Posted January 17, 2013 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 <noreply@hoosierhoopsreport.com>"; // Send mail('basketball@hoosierhoopsreport.com', 'Player database addition', $message, $headers); /* redirect user */ header("Location:http://hoosierhoopsreport.com/dbpe"); Quote Link to comment https://forums.phpfreaks.com/topic/273279-header-already-exist-error/ Share on other sites More sharing options...
premiso Posted January 17, 2013 Share Posted January 17, 2013 (edited) Please Read: http://forums.phpfreaks.com/topic/1895-header-errors-read-here-before-posting-them/ And see if that helps you understand / solve your problem. Edited January 17, 2013 by premiso Quote Link to comment https://forums.phpfreaks.com/topic/273279-header-already-exist-error/#findComment-1406459 Share on other sites More sharing options...
Andy123 Posted January 17, 2013 Share Posted January 17, 2013 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). Quote Link to comment https://forums.phpfreaks.com/topic/273279-header-already-exist-error/#findComment-1406466 Share on other sites More sharing options...
subhomoy Posted January 17, 2013 Share Posted January 17, 2013 Try writing on the top of the page <?php ob_start(); ?> It flushes the information that has been already sent to the server.... Hope it helps you... Quote Link to comment https://forums.phpfreaks.com/topic/273279-header-already-exist-error/#findComment-1406467 Share on other sites More sharing options...
PFMaBiSmAd Posted January 17, 2013 Share Posted January 17, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273279-header-already-exist-error/#findComment-1406519 Share on other sites More sharing options...
Jim R Posted January 18, 2013 Author Share Posted January 18, 2013 I moved the redirect (back to the entry page) into the 'else' part of the loop, so that eliminates the warning when the entry already exists. I'd like it to redirect (back to the entry page) even if the user inputs a name already in my database. Quote Link to comment https://forums.phpfreaks.com/topic/273279-header-already-exist-error/#findComment-1406567 Share on other sites More sharing options...
Jim R Posted January 18, 2013 Author Share Posted January 18, 2013 The redirect isn't working from within the loop. Quote Link to comment https://forums.phpfreaks.com/topic/273279-header-already-exist-error/#findComment-1406789 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.