S A N T A Posted April 28, 2008 Share Posted April 28, 2008 ok so I'm working on a blog and when you view a entry you are able to post a comment BUT when you post the comment it redirects you to the same bage but the layouts messed up and the only way to fix it is to refresh page and if you refresh it adds another comment. I tried redirecting to different page but it didn't work Heres the code: <?php session_start(); require("config.php"); ?> <?php if(isset($_GET['id']) == TRUE) { if(is_numeric($_GET['id']) == FALSE) { $error = 1; } if($error == 1) { header("Location: " . $config_basedir); } else { $validentry = $_GET['id']; } } else { $validentry = 0; } if($_POST['submit']) { $db = mysql_connect($dbhost, $dbuser); mysql_select_db($dbdatabase, $db); $sql = "INSERT INTO comments(blog_id, dateposted, name, comment) VALUES(" . $validentry . ", NOW(), '" . $_POST['name'] . "', '" . $_POST['comment'] . "');"; mysql_query($sql) or die(mysql_error(). " in $sql"); header("Location: " . $config_basedir . "/index.php"); } else { //code will go here } require("header.php"); if($validentry == 0) { $sql = "SELECT entries.*, categories.cat FROM entries, categories " . "WHERE entries.cat_id = categories.id " . "ORDER BY dateposted DESC " . " LIMIT 1;"; } else { $sql = "SELECT entries.*, categories.cat FROM entries, categories " . "WHERE entries.cat_id = categories.id AND entries.id = " . $validentry . " ORDER BY dateposted DESC LIMIT 1;"; } $result = mysql_query($sql)or die(mysql_error()); $row = mysql_fetch_assoc($result); echo "<h2>" . $row['subject'] . "</h2><br />"; echo "<i>In <a href='viewcat.php?id=" . $row['cat_id'] ."'>" . $row ['cat'] ."</a> - Posted on " . date("D jS FY g.iA", strtotime($row['dateposted'])) ."</i>"; if(isset($_SESSION['USERNAME']) == TRUE) { echo" [<a href='updateentry.php?id=" . $row['id'] . "'>edit</a>]"; } echo "<p>"; echo nl2br($row['body']); echo "</p>"; $commsql = "SELECT * FROM comments WHERE blog_id = " . $validentry . " ORDER BY dateposted DESC;"; $commresult = mysql_query($commsql); $numrows_comm = mysql_num_rows($commresult); if($numrows_comm == 0) { echo "<p>No comments.</p>"; } else { $i = 1; while($commrow = mysql_fetch_assoc($commresult)) { echo "<a name='comment" . $i . "'>"; echo "<h3>Comment by " . $commrow['name'] . " on " . date("D jS F Y g.iA", strtotime($commrow['dateposted'])) . "</h3>"; echo $commrow['comment']; $i++; } } ?> <table> <tr> <td> <h3>Leave a comment</h3> </td> </tr> <tr> <form action="<?php echo $SCRIPT_NAME . "?id=" . $validentry; ?>" method="post"> <td>Your Name</td> <td><input type="text" name="name"></td> </tr> <tr> <td>Comments</td> <td><textarea name="comment" rows="10" cols="50"></textarea></td> </tr> <tr> <td></td> <td><input type="submit" name="submit" value="Add Comment"></td> </tr> <tr> <td> <font size="-2">Do not post inapropriate comments or they will be deleted</font> </td> </tr> </form> </table> <?php require("footer.php"); ?> Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/103296-page-redirection/ Share on other sites More sharing options...
soycharliente Posted April 28, 2008 Share Posted April 28, 2008 Try adding an exit statement after your header redirect so the rest of the page will stop processing. Link to comment https://forums.phpfreaks.com/topic/103296-page-redirection/#findComment-529027 Share on other sites More sharing options...
S A N T A Posted April 28, 2008 Author Share Posted April 28, 2008 gives me this error: Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\viewentry.php:6) in C:\xampp\htdocs\viewentry.php on line 32 is this the right exit code? exit(); sorry I'm new to php (My sig) Link to comment https://forums.phpfreaks.com/topic/103296-page-redirection/#findComment-529031 Share on other sites More sharing options...
soycharliente Posted April 28, 2008 Share Posted April 28, 2008 It's just: exit; But you can't redirect if you've already outputted data. If you have, then your code logic is wrong. Link to comment https://forums.phpfreaks.com/topic/103296-page-redirection/#findComment-529050 Share on other sites More sharing options...
S A N T A Posted April 28, 2008 Author Share Posted April 28, 2008 i don't get it sorry how would i fix it? Link to comment https://forums.phpfreaks.com/topic/103296-page-redirection/#findComment-529054 Share on other sites More sharing options...
revraz Posted April 28, 2008 Share Posted April 28, 2008 Read the sticky up top that talks about Headers. Link to comment https://forums.phpfreaks.com/topic/103296-page-redirection/#findComment-529072 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.