Bobafart
07-15-2007, 05:48 PM
I have created a script that allows logged in users to "vote" on stories (much like Digg). It also checks to ensure that the user hasn't already voted on a story, if so the vote doesn't occur.
The problem that I am having is that the votes keep getting doubled. So if a user votes up or down on a story the total $voteValue ends up double either up or down.
I have echo'd the variable $voteValue throughout the script to error check and I can't seem to figure this out. I am using sessions to check to see if the user is logged in.
<?php
session_cache_expire(480);
session_start();
include("../inc/connect.php");
if(!($_SESSION[userid])) {
// the patient isn't logged in so don't do anything
echo "you need to log in";
} else {
// check to see if they have already voted
$query = "select id from anewsVotedStory where userid ={$_SESSION[userid]} && storyid=$storyid";
// echo '<p>'.$query.'</p>';
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
if($numrows == 0){ // the user hasn't voted already
$query = "select vote from anews2 where id=$storyid";
$result = mysql_query($query);
$voteValue = mysql_result($result,0,0);
// echo '<p>AJAX query: '.$query.'</p>';
// echo '<p>current Vote value: '.$voteValue.'</p>';
if($voteType == 0){
$voteValue = $voteValue+1;
// echo '<p>vote UP was pressed: incremented voteValue'.$voteValue.'</p>';
}elseif($voteType == 1){
$voteValue = $voteValue-1;
// echo '<p>vote DOWN was pressed</p>';
}
// echo '<p>current Vote value: <p>'.$voteValue;
$sqlInsert = "UPDATE anews2 SET vote=$voteValue WHERE id=$storyid";
// echo '<p>AJAX Insert: <p>'.$sqlInsert;
mysql_query($sqlInsert) or die(mysql_error($sqlInsert));
}
}
header ("Content-type: text/xml");
echo ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
echo ("<rss version=\"2.0\">");
echo ("<channel>");
echo ("<item>");
echo ("<voteValue>".$voteValue."</voteValue>");
echo ("</item>");
echo ("</channel>");
echo ("</rss>");
?>
the returned XML $voteValue keeps doubling... it seems that the increment/decrement if/else condition executes twice but I am not sure how or why...
The problem that I am having is that the votes keep getting doubled. So if a user votes up or down on a story the total $voteValue ends up double either up or down.
I have echo'd the variable $voteValue throughout the script to error check and I can't seem to figure this out. I am using sessions to check to see if the user is logged in.
<?php
session_cache_expire(480);
session_start();
include("../inc/connect.php");
if(!($_SESSION[userid])) {
// the patient isn't logged in so don't do anything
echo "you need to log in";
} else {
// check to see if they have already voted
$query = "select id from anewsVotedStory where userid ={$_SESSION[userid]} && storyid=$storyid";
// echo '<p>'.$query.'</p>';
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
if($numrows == 0){ // the user hasn't voted already
$query = "select vote from anews2 where id=$storyid";
$result = mysql_query($query);
$voteValue = mysql_result($result,0,0);
// echo '<p>AJAX query: '.$query.'</p>';
// echo '<p>current Vote value: '.$voteValue.'</p>';
if($voteType == 0){
$voteValue = $voteValue+1;
// echo '<p>vote UP was pressed: incremented voteValue'.$voteValue.'</p>';
}elseif($voteType == 1){
$voteValue = $voteValue-1;
// echo '<p>vote DOWN was pressed</p>';
}
// echo '<p>current Vote value: <p>'.$voteValue;
$sqlInsert = "UPDATE anews2 SET vote=$voteValue WHERE id=$storyid";
// echo '<p>AJAX Insert: <p>'.$sqlInsert;
mysql_query($sqlInsert) or die(mysql_error($sqlInsert));
}
}
header ("Content-type: text/xml");
echo ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
echo ("<rss version=\"2.0\">");
echo ("<channel>");
echo ("<item>");
echo ("<voteValue>".$voteValue."</voteValue>");
echo ("</item>");
echo ("</channel>");
echo ("</rss>");
?>
the returned XML $voteValue keeps doubling... it seems that the increment/decrement if/else condition executes twice but I am not sure how or why...