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>