Jump to content

Warning: Cannot modify header information


warrens0017

Recommended Posts

Hi,

 

I am having an issue with a warning that keeps popping up. I have edited the line it is saying but still getting the error.

 

Warning: Cannot modify header information - headers already sent by (output started at /Pages/Forums/comments.php:227) in /Pages/Forums/comments.php on line 88

 

Here are the two lines that the warning is saying

function setNote($conn) {
    if (isset($_POST['NoteSubmit'])) {
        $id = mysqli_real_escape_string($conn, $_POST['id']);
        $date = mysqli_real_escape_string($conn, $_POST['date']);
        $note = mysqli_real_escape_string($conn, $_POST['note']);

        $sql = "INSERT INTO note (id, date, note) 
        VALUES ('$id', '$date', '$note')";

        $result = mysqli_query($conn, $sql);

        header("Location: /Pages/Profile.php");  //This line has the error - LINE 88
    }
}

And the other

function getNote($conn) {
    $id = $_SESSION['id'];
    $sql = "SELECT * FROM note WHERE id='$id'";
    $result = mysqli_query($conn, $sql);

    while ($row = $result->fetch_assoc()) {
        echo "<div class='comment-box'><p><b>";
            echo "Note: </b><br>";
            echo nl2br($row['note']);
        echo "</p></div>
            <form class='delete-form' method='POST' action='".deleteNote($conn)."'>
                <input type='hidden' name='nid' value='".$row['nid']."'>
                <button type='submit' name='deleteNote'>Delete</button>
            </form>";  //This line has the error - LINE 227
    }
}

Now after I refresh the page, I see what i typed.

 

Hope someone has and answer.

 

Thanks!

Link to comment
Share on other sites

You cannot print any content at all before you send a header. No echo statements and no content outside of <?php ?> tags. Headers are always sent before the page content.

Link to comment
Share on other sites

See now I understand that but I have something very similar to the code that i wrote.

 

BUT, with the other code that is not different in anyway, I can submit a text to the database, and have it come write back out without any errors.

 

 

I did find out that with the code above that I can send one thing to the database and get it back without any issues, it only when I am trying to send more information to the database that I get the error. And that is where I am at and I am confused. That a very similar code work but not this one.

Link to comment
Share on other sites

You haven't shown enough code to determine what the issue is. Where are the setNote() and getNote() functions being called?

 

You cannot call setNote() after you have called getNote().

Link to comment
Share on other sites

<?php

      if (isset($_SESSION['id'])) {
        echo "<table id='Table_note'>";
        echo "<tr class='noteTable'>";
        echo "<th>Notes</th>";
        echo "<th></th></tr>";
        getNote($conn);
        echo "</table><br>";
        echo "<form method='POST' action='".setNote($conn)."'>
                <input type='hidden' name='id' value='".$_SESSION['id']."'>
                <input type='hidden' name='date' value='".date('Y-m-d')."'>
                <textarea name='note'></textarea><br>
                <button class='button-YT' type='submit' name='NoteSubmit'>Sent Note</button>
              </form>";
      } else {
        echo "<p>You Need to Login</p>";

      }
?>

This is where the setNote() and getNote() is placed.

Edited by warrens0017
Link to comment
Share on other sites

You can't put setNote() there because it doesn't return anything and it sends headers.

 

setNote() should only be called when you're not printing the form, it should be at the very beginning of your code before anything is printed.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...