Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help: To see if a user voted or not (MySQL & PHP)
Old 11-11-2008, 11:00 PM Help: To see if a user voted or not (MySQL & PHP)
Junior Talker

Posts: 1
Trades: 0
Hi, first time poster, and I'm not entirely experienced...

Here is the situation: I am in process of creating a website where users can vote items, much like Digg. I am using MySQL and PHP. In my mysql database, I have two tables, one of them for users and the other for the items which users can vote on.

In order to vote, a user clicks on a link, with the href="voteup.php?id=3" where 3 could be any number - it's the id of the item they are voting on. This vote adds one to the row of the item.

The problem: I don't know how to keep track of what items each user voted on. And since I can't keep track, I cannot prevent them from voting infinitely on each item. Does anyone have any ideas on what I could do to keep track of what items the user votes?

I was considering adding an additional column on the table for the items, with a string of USER IDs, but I'm not entirely sure where to go with that.

Thanks in advance!
silentsteps is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-11-2008, 11:31 PM Re: Help: To see if a user voted or not (MySQL & PHP)
Extreme Talker

Posts: 246
Trades: 3
If each user will have their own user ID, then that is a perfect idea.

SQL:
Code:
create table votes (
   itemid int,
   userid int,
   voteup int
)
Then to prevent multiple votes from the same user:
PHP Code:
$sql "
   select
      count(*) as uservotes
   from
      votes 
   where
      itemid = 
{$itemid}
      and userid = 
{$userid}
"
;
$sqlresult mysql_query($sql) or die("Error counting votes.");
$row mysql_fetch_array($sqlresult);
if 
$row["uservotes"] >= {
   
//this user has already voted on this item
   
echo "You silly cat.  You already voted.";
} else {
   
//this user has not yet voted
   
$sql "
      insert into votes (
         itemid, 
         userid, 
         voteup
      )
      values (
         
{$itemid}
         
{$userid}
         1
      )
   "
;
   
$sqlresult mysql_query($sql) or die("Error inserting voteup.");

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by CouponGuy; 11-11-2008 at 11:34 PM.. Reason: spacing/punctuation
CouponGuy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help: To see if a user voted or not (MySQL & PHP)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.10112 seconds with 12 queries