Jump to content

Warning: Cannot modify header information


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 post
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 post
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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...