Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Obed Lorisson
Front End Web Development Techdegree Student 6,151 PointsIssue with PHP header
I need some help with this error :"Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\mizikmix\inc\header.php:104) in C:\xampp\htdocs\mizikmix\inc\admin.php on line 63, when i read on stackoverflow , something with whitespace . i don't know how to fix it , it's really annoying . Any help i will appreciate
18 Answers
Randy Hoyt
Treehouse Guest TeacherShort Answer: I think you need to run admin.php before header.php.
Long Answer: When a browser makes a request to a server for a web page, the server responds with two types of information: (1) the headers for the response and (2) the content (typically HTML) for the requested page. These headers can be set in PHP with the header() command. The most common use for this is to redirect the browser to another page; that's what the admin.php file is doing.
Headers must be set in PHP before any content gets output. Content can be output with an echo command or with any text outside of a PHP tag. Once that happens, headers can no longer be set: it's just too late.
It's hard to tell what's happening looking only at admin.php: the headers are getting sent in header.php. My guess is that the header.php file outputs some HTML before admin.php runs. You need to execute the code in it before any HTML gets output in header.php. Do you have control over the order these files get executed?
James Barnett
39,199 Points@Elizabeth - JSFiddle is only for HTML/CSS/Javascript not PHP.
However there's an app for that, use a PHP Sandbox
Caroline Hagan
12,612 PointsObed, can you post the code you have in admin.php ?
Obed Lorisson
Front End Web Development Techdegree Student 6,151 PointsHey Caroline here's the gist
Caroline Hagan
12,612 PointsThat link just gives me a blank screen o_O
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points@caroline , it seems i can't share the code , it a little bit long , don't want to paste it here
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points@Caroline
i gonna send you the file in the facebook group
Caroline Hagan
12,612 PointsOkie dokie
James Barnett
39,199 PointsYou can also use a http://pastebin.com
ecp
838 PointsPastebin or Jsfiddle will work too. Or go through the Facebook group ;P Glad to see the teamwork goin on! Woop woop!
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Pointshttp://codepad.viper-7.com/e3heVJ here the codepad version , thanks James Barnett hope it's working
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points@Randy Hoyt ok i got what you said , this is the way i have it setup <?php include('inc/db_connect.php'); include('inc/admin.php'); include('inc/header.php') ?> In the error output it said , the issue is in the Header ,but the header.php is all html , there's no PHP code.so this thing really confuse.
Randy Hoyt
Treehouse Guest TeacherActually, the HTML is the problem. The error happens in admin.php with the header() command. The error tells you that it is too late to use a header() command because there was already some HTML in header.php. Once there's any HTML to get sent to the browser, then headers can no longer be changed. Headers have to come before the content of the page. Once an echo statement in PHP happens or once there's any non-PHP code (HTML or plain text outside of PHP tags), then any header command will produce an error.
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Pointscan i send you a link that you can see it live
Randy Hoyt
Treehouse Guest TeacherYes, please share a link. I'll take a look at it tomorrow. We won't be able to see the server-side code, but it may help.
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points@Randy Hoyt , This is the link , http://www.mizikbox.com/entry.php , this link is control by the Admin.php
the header prob seems resolve because i don't have the warning anymore about the header , i don't know what i tweak , seems working now , but how i can prevent when i press back for the form not submitting again , cause i end up having duplicates records in the database .
Other questions: in the same form i have an update button to update the records on the database, i use the MySQL update command in the PHP script , but seems when i try to update ,it simply create a new record instead of update the record mentions by the WHERE clause.
in ruby before update , you have to retrieve the records first and apply the update and save. i'masking myself if the same rule apply for PHP .
here's the update PHP script.
if(isset($_GET['updatevideo'])) { include'update.html.php'; exit(); } //updating record if (isset($_POST['VideoTITLE']) && isset($_POST['ByArtist']) && isset($_POST['GroupName']) && isset($_Post['URL'] )) { try { $sql = 'UPDATE videoclip SET VideoTITLE = :VideoTITLE, ByArtist = :ByArtist, GroupName = :GroupName, URL = :URL WHERE ID = ID;';
$s = $pdo -> prepare($sql); $s -> bindValue(':VideoTITLE',$_POST['VideoTITLE']); $s -> bindValue(':ByArtist',$_POST['ByArtist']); $s -> bindValue(':GroupName',$_POST['GroupName']); $s -> bindValue(':URL',$_POST['URL']); $s -> execute(); } catch(PDOException $e) { $error = 'error adding submitted data' . $e-> getMessage(); include 'error.html.php'; exit(); } header('Location: entry.php'); exit(); }
Randy Hoyt
Treehouse Guest TeacherI talk about preventing the form re-submission over in the PHP tutorial videos about the contact form:
The video called "Redirecting After A Form Submission" discusses this.
Randy Hoyt
Treehouse Guest TeacherFirst of all, I think your SQL statement has an error in it. The WHERE clauses says this:
WHERE ID = ID
That will be true for every record, so it looks like you will be loading the data submitted through the form into every record. I don't think that's what you are intending to do, are you? Perhaps you are trying to update only one record? You'll need to have a variable with the ID for that one record somewhere.
Now to your question: When you say "in Ruby," I trust you mean Ruby on Rails, right? Ruby on Rails is a framework that makes development faster; with frameworks, you are not typically executing your SQL statements directly against the database. The PHP code you have included, however, is directly executing a SQL query directly. You wouldn't have to retrieve any records first. If you execute an UPDATE SQL statement, it won't create a new record.