Jump to content

Header and form question


Wintergreen

Recommended Posts

I made a pretty simple blog over the past few days, but have had one problem. I have the page where you write your post, and then you submit it which takes you to the php page that processes the info, adds it to the db etc.  However, when I try to use header("location: index.php"); at the end of the db insertions I get this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/.alias/synergistic/potentiates.com/pandafactory/addcomment.php:16) in /home/.alias/synergistic/potentiates.com/pandafactory/addcomment.php on line 34

The code for the page is here:
[code]<?
$postid = $_GET['id'];
echo "<a href=comment.php?id=" . $postid . ">Your comment has been added, go see it!</a>";
include 'db.php';

$post_body = $_POST['bodytext'];
$post_time = date("Y-m-d H:i:s");
$poster_name = $_SESSION['screenname'];

$sql = "SELECT comment_number FROM posts WHERE postid = '$postid'";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
$addposts = $row['comment_number'] + 1;
}
mysql_query("UPDATE posts SET comment_number = '$addposts' WHERE postid = '$postid'") or die(mysql_error());

//Enter info into the db
mysql_query("INSERT INTO comments (postid, post_body, post_time, poster_name) VALUES('$postid','$post_body','$post_time','$poster_name')") or die(mysql_error());
header("Location: index.php");
exit();
?>
[/code]

So, instead of using the header I put up a simple anchor link to the index page above the php processing so the users can click it to go back, but this isn't an ideal solution.  The other problem when how I'm currently doing it is if you refresh while on the processing page you end up with double posts.  How can I stop this from happening?


My other question concerns a page I'm making to manage posts with forms instead of using SSH to manually doing it.  My idea was to list each post within a form, each post having a corresponding checkbox that you'd check if you wanted the post deleted, etc.  So if you wanted five posts deleted, you'd check the five, click submit and on the next page it would ask one last time if you want to continue and then actually delete them.  Now, the problem is that I know how to check for values on the checkboxes, but how do I pass postid of the post to be deleted to the next page? 

When I set up pages for editing it was easier, because the link to the edit page was edit.php?id=postid, but when I don't know which posts are the ones the user will want to delete this method seems like it doesn't work. 

Anyway, any help would be greatly appreciated.
Link to comment
Share on other sites

[code]

<?php

$postid = $_GET['id'];
echo "<a href=comment.php?id=" . $postid . ">Your comment has been added, go see it!</a>";
include 'db.php';

$post_body = $_POST['bodytext'];
$post_time = date("Y-m-d H:i:s");
$poster_name = $_SESSION['screenname'];

$sql = "SELECT comment_number FROM posts WHERE postid = '$postid'";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
$addposts = $row['comment_number'] + 1;
}

if($_POST['submit']){

mysql_query("UPDATE posts SET comment_number = '$addposts' WHERE postid = '$postid'") or die(mysql_error());

//Enter info into the db
mysql_query("INSERT INTO comments (postid, post_body, post_time, poster_name) VALUES('$postid','$post_body','$post_time','$poster_name')") or die(mysql_error());
header("Location: index.php");
exit();
}
?>

[/code]
Link to comment
Share on other sites

meaning this script has to be before your HTML

mysql_query("INSERT INTO comments (postid, post_body, post_time, poster_name) VALUES('$postid','$post_body','$post_time','$poster_name')") or die(mysql_error());
header("Location: index.php");
exit();
}
?>

hence the headers cant be sent again so just replace the header("Location: index.php");

with

echo "<meta http-equiv=\"refresh\" content=\"2;url=http://webdesign.about.com\">";

[code]
<?php

$postid = $_GET['id'];
echo "<a href=comment.php?id=" . $postid . ">Your comment has been added, go see it!</a>";
include 'db.php';

$post_body = $_POST['bodytext'];
$post_time = date("Y-m-d H:i:s");
$poster_name = $_SESSION['screenname'];

$sql = "SELECT comment_number FROM posts WHERE postid = '$postid'";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
$addposts = $row['comment_number'] + 1;
}

if($_POST['submit']){

mysql_query("UPDATE posts SET comment_number = '$addposts' WHERE postid = '$postid'") or die(mysql_error());

//Enter info into the db
mysql_query("INSERT INTO comments (postid, post_body, post_time, poster_name) VALUES('$postid','$post_body','$post_time','$poster_name')") or die(mysql_error());
echo "<meta http-equiv=\"refresh\" content=\"2;url=http://webdesign.about.com\">";
exit();
}
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.