Jump to content

Voting script assistance required.


esoteric

Recommended Posts

Hello,

 

Im trying to create a simple voting script for my site, i been trying different things but cannot get it too function properly. The idea is you click the 'thumbs up' image to add 1 to the 'likes' value and thumbs down to add '1' to the dislikes. Sound simple but its giving me headaches.

 

Would really appreciate it if someone could take a quick look at my script. The table is called 'youtube_rating' (because thats the look i was going for) and in the table is 4 columns, id (aut0_increment), liked (counts liked votes), disliked (counts dislikes) and item_num which is the id for teh product the rating is for.

 

<? if ( $row['rating'] == 'YES' ) { 
require ("../rating/voting/dbcon.php");

	$result1	= mysql_query("select * from youtube_rating WHERE item_num='".$page_id."'");
	$row		= mysql_fetch_array($result1);
	$dislike	= $row['dislike'];
	$like		= $row['liked'];

if (isset($_POST['submitted'])) 
{

require_once ("../database/functions.php");

	$userip		= $_SERVER['REMOTE_ADDR'];
	$id			= safe($_POST['id']);
	$vote		= '1';


	// check if user already voted
	$result = mysql_query("SELECT * FROM youtube_ip WHERE userip='$userip' AND item_num ='".$id."'");
	$num 	= mysql_num_rows($result);

    // if already voted show error
if (isset($_POST['submitted']) && ($num > 0)) 
 	{ 
		echo '<div class="alreadyvoted">You have already voted!</div>';

 	} 

    // add like
if (isset($_POST['submitted']) && ($_POST['submitted'] == 'like') && ($num == 0)) 
	{ 
		// add 1 to value
		$liked_value 	= mysql_query("SELECT liked FROM youtube_ip WHERE item_num ='".$id."'");
		$liked_total	=	$liked_value + $vote;

		$query = "update youtube_rating set liked = '".$liked_total."' where item_num ='".$id."'";
		echo '<div class="ilikethis">You like this!</div>';
     	} 

    // add dislike
if (isset($_POST['submitted']) && ($_POST['submitted'] == 'dislike') && ($num == 0))  
	{ 
		// add 1 to value
		$dislike_value 	= mysql_query("SELECT dislike FROM youtube_ip WHERE item_num ='".$id."'");
		$dislike_total	=	$dislike_value + $vote;

		$query = "update youtube_rating set dislike = '".$dislike_total."' where item_num ='".$id."'";
		echo '<div class="idislikethis">You Dislike this!</div>';
	}

// log ip
// if (isset($_POST['submitted']) && ($num == 0))  
//	{ 	
//		mysql_query ("insert into youtube_ip (userip, item_num) values ('".$userip."', '".$id."')");
//	}
}
?>

<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="48%" align="center">
    <form action="" method="post">
    <input name="id" type="hidden" value="<? echo $page_id ?>" />
    <input name="submitted" type="hidden" value="like" />
<button type="submit" class="like_button" name="like" id="like" >
<img src="../rating/voting/pixel-vfl73.gif" alt=""> 
<span><?php echo $like?></span>
    </button>
</form>
    </td>
    <td width="52%">
    <form action="" method="post">
    <input name="id" type="hidden" value="<? echo $page_id ?>" />
    <input name="submitted" type="hidden" value="dislike" />
<button type="submit" class="dislike_button" name="dislike" id="dislike" >
<img src="../rating/voting/pixel-vfl73.gif" alt=""> 
    <span><? echo $dislike?></span>
</button>
</form>
    </td>
  </tr>
</table>

<? 
}
?>

 

The problem is the vote does not add up or update the database, also it doesn't seem to be checking if the user has voted or not, it just attempts (but fails) to add a vote no matter what

 

Thanks for any help.

Link to comment
Share on other sites

Isolate the code which does not work and then post that. Also if you do that, you will probably find the fault yourself too.

Posting fairly large pieces of code puts people off answeing 'cos they want to run in....post a reply... then run off again and if they have to stop, work out what is going on in your program and then debug it, they may not bother. Sorry if you dont like this answer, but this is just the way life works.

 

My suggestion is to set a few literals and run it with those and see if it works. If it does not, then debug it with the literals and then comment them out once that is running OK.

Link to comment
Share on other sites

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.