...

View Full Version : Increment/Decremnting by 1 results in increment/decrement of 2



Bobafart
07-15-2007, 06: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...

CFMaBiSmAd
07-15-2007, 07:01 PM
I did not read through the logic in the post, but are you using any url rewriting on your server to resolve the address for the web page with that logic in it?

If so, there might be a missing trailing / on a path, which causes two requests to be made to the web server.

phpandmysql
07-15-2007, 08:36 PM
I could be wrong but this line


$voteValue = mysql_result($result,0,0);

should be


$voteValue = mysql_result($result,0,"vote");

Does that help?

Fumigator
07-15-2007, 08:48 PM
phpandmysql: You can do it either way.

http://us2.php.net/manual/en/function.mysql-result.php

phpandmysql
07-15-2007, 09:36 PM
phpandmysql: You can do it either way.

http://us2.php.net/manual/en/function.mysql-result.php

Oh yeah duh. I didn't see the select vote from. I hav a habit of select * from and then drawing in the result.

okay Bobafart try this:
I know it shouldn't matter but it doesn't hurt to try and see if it works:
replace your current if with this.



REPLACE THIS:

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));





WITH THIS:

if($voteType == 0){
$sqlInsert = "UPDATE anews2 SET vote=vote+1 WHERE id=$storyid";
mysql_query($sqlInsert) or die(mysql_error($sqlInsert));
}elseif($voteType == 1){
$sqlInsert = "UPDATE anews2 SET vote=vote-1 WHERE id=$storyid";
mysql_query($sqlInsert) or die(mysql_error($sqlInsert));
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum