Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    Increment/Decremnting by 1 results in increment/decrement of 2

    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.

    Code:
    <?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...

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,018
    Thanks
    2
    Thanked 313 Times in 305 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Jul 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    I could be wrong but this line

    PHP Code:
    $voteValue mysql_result($result,0,0); 
    should be

    PHP Code:
    $voteValue mysql_result($result,0,"vote"); 
    Does that help?
    PHP and MYSQL
    $string = "3Ip*hKEpanKI#8dmUys&*KqlIJ*P8D";
    $new_string = ereg_replace("[^a-z]", "", $string);
    echo "<a href=http://www.$new_string.org>$new_string</a>";

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    phpandmysql: You can do it either way.

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

  • #5
    New Coder
    Join Date
    Jul 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    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:
    PHP Code:
    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:
    PHP Code:
    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));
                    } 
    PHP and MYSQL
    $string = "3Ip*hKEpanKI#8dmUys&*KqlIJ*P8D";
    $new_string = ereg_replace("[^a-z]", "", $string);
    echo "<a href=http://www.$new_string.org>$new_string</a>";


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •