1

Hey i'm learning PHP with some tutorials from lynda.com. And in the tutorial we are making a CMS. I follow the steps as in the tutorial even got the file from lynda.com and its perfect the same but in the tutorial it works and when i do it the header location doesn't work help.

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php error_reporting(E_ALL); ini_set('display_errors', 'On'); ?>

<?php
$menu_name = $_POST['menu_name'];
$position = $_POST['position'];
$visible = $_POST['visible'];
?>
<?php
$query = "INSERT INTO subjects (
            menu_name, position, visible
        ) VALUES (
            '{$menu_name}', {$position}, {$visible}
        )";
$result = mysql_query($query, $connection);
if ($result) {
    // Success!
    header("location:content.php");
    exit;
} else {
    // Display error message.
    echo "<p>Subject creation failed.</p>";
    echo "<p>" . mysql_error() . "</p>";
}
?>

<?php mysql_close($connection); ?>

the error that i get is as followed

Warning: Cannot modify header information - headers already sent by (output started at      /Applications/MAMP/htdocs/Widgetcorp/create_subject.php:5) in /Applications/MAMP/htdocs/Widgetcorp/create_subject.php on line 19
aizaz
  • 3,056
  • 9
  • 25
  • 57
Garneto
  • 25
  • 1
  • 9
  • you shouldn't seperate your php in different php tags since you're not using any other data. everything can be wrapped in tag. I think the header error is comming from the empty line between the two tags. (line 4) - see for: http://stackoverflow.com/questions/8028957/headers-already-sent-by-php – Kristof Feys Aug 20 '13 at 11:00

4 Answers4

3

Make your code this way

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 'On');

require_once("includes/connection.php");
require_once("includes/functions.php");

$menu_name = $_POST['menu_name'];
...

without all this unnecessary opening/closing PHP tags.

Note that line 5 was clearly indicated in the error message as a cause. Always read error messages. They are helpful.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

This is because you have already did some output in your script. Try use single open/close php tags pair in script - at begin and at the end.

And use right redirect header syntax:

header('Location: http://www.example.com/');

Location from upper case, whitespace after

Brane
  • 3,257
  • 2
  • 42
  • 53
zeromodule
  • 43
  • 6
0

you have white-space between the following lines. remove it so it will potentially work

<?php error_reporting(E_ALL); ini_set('display_errors', 'On'); ?>

<?php
DevZer0
  • 13,433
  • 7
  • 27
  • 51
0

You propably have some "echo" Call (or other function that prints text) in one of your scripts you call with "require_once()".

Here the text from the PHP Doc:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

user619
  • 119
  • 5