0

On this page I have a query to select then display a message, a query to delete a message and a query to add a message. When the query to delete a message and add a message run successfully I am redirecting back to the same page so the user can see the upadate. Because I need to pass a few variables when I do this I am using header redirect.

$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/discussion.php?caseid=' . $case_id.'&did='.$discussion_id.'&user_id='.$user_id; header('Location: ' . $home_url);

I noticed though that my second redirect ( the one that is used to add message) gives me this

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\casekeeper\discussion.php:85) in C:\xampp\htdocs\casekeeper\discussion.php on line 126

after I add 3-4 messages. Can I not have to header redirect messages on the same page, did I set me script up incorrectly or am I just on the totally wrong path here?

    $query2 = "SELECT user_profile.first_name, user_profile.last_name, discussion_messages.message_text, discussion_messages.message_id FROM `user_profile` INNER JOIN discussion_messages ON (user_profile.user_id = discussion_messages.user_id) WHERE discussion_messages.discussion_id = '" . $_GET['did'] . "' ORDER BY discussion_messages.message_id DESC";
    $data2 = mysqli_query($dbc, $query2); 
    while ($row2 = mysqli_fetch_array($data2)) {
    $message_id = $row2['message_id'];
    ?>
    <div class="row">
      <div class="col-xs-4 col-md-2">

      </div>
      <div class="col-xs-8 col-md-10 discussion_title">
        <?php echo '<h5 class="discussionname">' . $row2['first_name'] . ',' . $row2['last_name'] . '</h5>';?>
        <?php echo '<h5 class="">' . $row2['message_text'] . '</h5>';?>           
        <h6 class="pull-right">Jan. 03 </h6>
        <div>
    <?php 
      if (isset($_POST['delete'])) {
      // Grab the profile data from the POST

        $messageid = mysqli_real_escape_string($dbc, trim($_POST['messageid']));
        $queryd = "DELETE FROM `discussion_messages` WHERE message_id = '$messageid' ";
        mysqli_query($dbc, $queryd)
        or die("Unable to connect");
        mysqli_close($dbc);

        $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/discussion.php?caseid=' . $case_id.'&did='.$discussion_id.'&user_id='.$user_id;
         header('Location: ' . $home_url);
          }
          ?>
          <div>
             <form method="post">
              <?php echo '<input type="hidden" value="'.$row2['message_id'].'" name="messageid" />'; ?>
              <input type="submit" name="delete" class="btn btn-info btn-sm" value="delete"/>
            </form>
          </div>
        </div>
      </div>
    </div>
    <?php 
        };

        ?>
    <br>
    <hr>
    <?php

    if (isset($_POST['submit'])) {
      // Grab the profile data from the POST
        $message = mysqli_real_escape_string($dbc, trim($_POST['message']));
        $query3 = "INSERT INTO `discussion_messages`(`message_text`, `discussion_id`, `user_id`, `message_date_time`, `case_id`) VALUES ('$message','$discussion_id','$user_id', NOW(), '$case_id')";
        mysqli_query($dbc, $query3)
        or die("Unable to connect");
        //Confirm success with the user
        mysqli_close($dbc);

        $update_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/discussion.php?caseid=' . $case_id.'&did='.$discussion_id.'&user_id='.$user_id;
         header('Location: ' . $update_url);

      };

      ?>
    <div class="row col-md-10 col-md-offset-1">
      <div class="well well-lg">
          <form role="form" method="post">
          <div class="form-group">
            <label class="sr-only" for="fact_text">Fact Text</label>
            <textarea name="message" class="form-control" rows="10" placeholder="Put note here"></textarea>
          </div>
          <br>

          <input type="submit" value="Add to discussion" name="submit" class="btn btn-info btn-sm" />

          </form>
      </div>
    </div>
Sam Brody
  • 33
  • 1
  • 5
  • 1
    As for the thousands of exact dupes of your error message: you've performed output **BEFORE** you tried doing a `header()` call – Marc B Feb 06 '14 at 18:08
  • Everytime I try to put the header call above any output it says it cant find the page, if I want to redirect back to the page im already on but cant use header.....is there another way to do this so I can still pass variables in URL or even in post? – Sam Brody Feb 06 '14 at 18:41

1 Answers1

0

I think you can try a simple way for redirect your page :

echo "<script>window.location.href = $update_url</script>";

Or

echo "<script>window.location.raplace('$update_url')</script>";

Remember that header() must be called before any actual output is sent.(Reference)

Griwes
  • 8,805
  • 2
  • 43
  • 70
Saman Gholami
  • 3,416
  • 7
  • 30
  • 71
  • IN the end that was the problem, I ended up moving the header inside the php logic to the top of my page and it was fixed – Sam Brody Feb 07 '14 at 15:12