Jump to content


Photo

Header and form question


  • Please log in to reply
5 replies to this topic

#1 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 03 September 2006 - 03:22 AM

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:
<?
$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();
?>

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.

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 September 2006 - 03:31 AM


<?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();
}
?>


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 03 September 2006 - 05:20 AM

I'm still getting the same error as before with the if($_POST['submit']) added in

#4 Demonic

Demonic
  • Members
  • PipPipPip
  • Advanced Member
  • 562 posts

Posted 03 September 2006 - 05:49 AM

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\">";

<?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();
}
?>


#5 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 04 September 2006 - 01:44 AM

Alright, thanks for the reply, it now works without problems, no double posts either.

Anyone have any ideas on the form value passing?

#6 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 04 September 2006 - 02:21 PM

Bump, I'm sure someone has an idea of how it might be possible




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users