Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7650889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:22:05+00:00 2026-05-31T11:22:05+00:00

I am setting up a voting program where I want to limit users to

  • 0

I am setting up a voting program where I want to limit users to one vote up. I have two tables: (a) subject table and (b) vote table.

**Subject_Table**              

SID   Subject   Total_Votes          
1     Cows      5     
2     Chickens  3     


**Vote_Table**

VID   Subject   User    Voteup           
1     Cows      John    1     

Total votes equal votes from all users. Voteup can only equal 1 if an subject is voted on. I have made a unique value pair with Subject and User with the following statement without any problems:

ALTER TABLE Vote_Table ADD UNIQUE limitvote(Subject,User);

When a subject is voted on for the first time the following queries execute:

$sql="INSERT INTO Vote_Table (Subject, User,Voteup) VALUES ('$Subject', '$User', '$Voteup') ON DUPLICATE KEY UPDATE up=1";
$q = "UPDATE Subject_Table SET Total_Votes = $votes_up= //current votes plus 1;

Even if John votes twice the vote will always equal 1 in the Vote_Table due to ON DUPLICATE KEY UPDATE up=1. But this is not so in the Subject_Table. Without a constraint, John could vote infinitely. The constraint must be the subject-user pair. Constraints cannot be only Subject or User since John can vote for other subjects and other users can vote for Cows.

How can I check Vote_Table to see if the unique Subject-User pair (Cows and John)) exists before I update Subject_Table?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:22:06+00:00Added an answer on May 31, 2026 at 11:22 am

    You can incorporate it into the WHERE clause:

    UPDATE Subject_Table
       SET Total_Votes = Total_Votes + 1
     WHERE Subject = ...
       AND NOT EXISTS
            ( SELECT 1
                FROM Vote_Table
               WHERE Subject = ...
                 AND User = ...
            )
    ;
    

    That said, I’m not sure that you really need Subject_Table at all; you can create a view on the Vote_Table that will give you the same information, without having to create separate table. (See the the Wikipedia article on Database normalization.) That could look like this:

    CREATE VIEW Subject_View AS
    SELECT Subject,
           COUNT(1) AS Total_Votes
      FROM Vote_Table
     GROUP
        BY Subject
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a voting system for articles. Articles are stored in 'stories' table and
I have a site that allows users to vote on images. Each image has
Setting up ASP.net MVC with Linq2SQL or Entity Framework's context to have scaffolding work
Setting up new git installations. On one Windows laptop, I'm running (under cygwin): git
The Setting: The program in question tries to post form data via an AJAX
I'm using Users Points Voting API module to combine User Points module and Fivestar
ive been giving the task at work of setting up an awards voting system,
I'm new to Django and trying to implement a voting system between two images.
Setting up a Samba share has got to be one of the most complex
Setting up CI within Microsoft Team Foundation Server, I have a build that will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.