Jump to content

Why can't I unset($_POST)


suttercain

Recommended Posts

Hello everyone,

 

I have a small script I wrote so people can vote on a page:

 

<?php
//voting system
		  function voted() {
		  echo "Thank you for voting!"; }
		  $voting = "SELECT * FROM comicvote WHERE comic_id ='" . $row['comic_id'] . "'";
		  $votes = mysql_query($voting) or die (mysql_error());
		  $sum = 0;
		  while ($rowvote = mysql_fetch_row($votes)) {
		  $sum += $rowvote['0'];
		  } 
		  $average = $sum/5 *100;
		  $format = number_format($average,2);
		  if ($votes == !NULL) { 		
		  		if ($average >= 1 && $average <= 59.99) {
					$rating = "<img src='../images/comics/grade_f.gif'>";
				} elseif ($average==0) {
					$rating = "Be The First To Vote!<br>";
				} elseif ($average >= 60.00 && $average <= 69.99) {
					$rating = "<img src='../images/comics/grade_d.gif'>";
				} elseif ($average >= 70.00 && $average <= 79.99) {
					$rating = "<img src='../images/comics/grade_c.gif'>";
				} elseif ($average >= 80.00 && $average <= 89.99) {
					$rating = "<img src='../images/comics/grade_b.gif'>";
				} else {
					$rating = "<img src='../images/comics/grade_a.gif'>"; }
			echo $rating;
		  } 
		  $total = mysql_num_rows($votes);
		  if ($total > 0) {
		  	echo "<br>Based on $total votes!"; }
		  if ($vbulletin->userinfo['usergroupid'] == '6' || 
		  	  $vbulletin->userinfo['usergroupid'] == '2') {
			  if (!isset($_POST['submit'])) { 
			  	voteNow();
		  		} else {
				$rating = $_POST['rating'];
				$comic_id = $row['comic_id'];
				$sent = mysql_query ("INSERT INTO comicvote (rating, comic_id) VALUES ('$rating', '$comic_id')") or die(mysql_error());
                                        unset($_POST['submit']);//Trying to unset the $_POST, not working though!
				voted ();
		  		}
			} else {
			echo "<a href='/forums/register.php?s='><br>Login to Vote</a>";
				}
		  
		  
		  function voteNow() {
		  ?>
		  	<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
			     <div align="center">
				  <select name="rating">
			 	     <option value="5">A</option>
				     <option value="4">B</option>
				     <option value="3">C</option>
				     <option value="2">D</option>
				     <option value="1">F</option>
			       </select>
		  		  <input type="submit" name="submit" value="Vote">
				   </div>
		  	</form>
		  <?php 
		  } 
		  ?>

 

The issue I am having is that after the vote in inserted into the MySQL database I want the $_POST['submit'] to be unset or destroyed. This isn't working, because each time the page is reloaded it votes again and again.

 

Anyone have any suggestions? Thanks.

 

SC

Link to comment
Share on other sites

instead of unsetting $_POST['submit']

 

before this line:

  echo "Thank you for voting!"; }

 

add

$submitted = $_POST['submit'];

if ($submitted == $_POST['submit']) {

//do the query and echo the results

$submitted = NULL;

} //close this if statement

 

then change

if (!isset($_POST['submit'])) {

 

to

if ($submitted != $_POST['submit']) {

 

That should work - maybe

Link to comment
Share on other sites

I tried running this, but of course got that the headers have already been sent:
[code]
<?php
if (!isset($_POST['submit'])) { 
			  	voteNow();
		  		} else {
				$rating = $_POST['rating'];
				$comic_id = $row['comic_id'];
				$sent = mysql_query ("INSERT INTO comicvote (rating, comic_id) VALUES ('$rating', '$comic_id')") or die(mysql_error());
				unset($_POST['submit']);
				voted ();
				header("comicvote.php");
		  		}
?>

 

ERROR:

Warning: Cannot modify header information - headers already sent by (output started at /home/superman/public_html/comics/view_comics1.php:13) in /home/superman/public_html/comics/comicvote.php on line 41

 

Other than using

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">

I am not sure how to do this.[/code]

Link to comment
Share on other sites

if your going to do a redirect, use javascript and not a header

 

just change that line to this:

 

echo "<br />Redirecting...<script language='javascript'>setTimeout('window.location=\"./comicvote.php\"',2000);</script>";

Link to comment
Share on other sites

Hi yzerman,

 

Thanks for the suggestions. I tried both,

 

nstead of unsetting $_POST['submit']

before this line:
  echo "Thank you for voting!"; }

add
$submitted = $_POST['submit'];
if ($submitted == $_POST['submit']) {
//do the query and echo the results
$submitted = NULL;
} //close this if statement

then change
if (!isset($_POST['submit'])) {

to
if ($submitted != $_POST['submit']) {

 

and also using the javascript.

 

I am still having the same issue. I hit reload, it counts the vote over and over.

Link to comment
Share on other sites

because when you hit refresh, you (the client) are resending the form. The only way around this is to redirect to another page.

 

Now because this is a voting system, I imagine you are inputting a vote only for registered users. What you can do in that case, is add a field to your table if it doesn't exist and store the username. Then check for a vote on that particular subject from that username.

 

If you are not requiring registered users, store the ip of the user with the vote - then before it is submitted, check the table for that IP address submitting a vote, and stop the query if that IP has already submitted a vote for that particular subject.

Link to comment
Share on other sites

Hi yzerman,

 

Yeah I had already started that and added a column to my MySQL table for the user id. I think I may go that route also, cause that means they can't come back another day and revote.

 

I thank all three of you for helping me out. I'll try the user one time vote thing.

 

Thanks for your time.

 

SC

Link to comment
Share on other sites

  • 2 years later...
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.