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

  • Home
  • SEARCH
  • 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 3243498
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:26:26+00:00 2026-05-17T18:26:26+00:00

Just to give you an example: I have a PHP script that manages users

  • 0

Just to give you an example:

I have a PHP script that manages users votes.

When a user votes, the script makes a query to check if someone has already voted for the same ID/product. If nobody has voted, then it makes another query and insert the ID into a general ID votes table and another one to insert the data into a per user ID votes table. And this kind of behavior is repeated in other kind of scripts.

The question is, if two different users votes simultaneously its possible that the two instances of the code try to insert a new ID (or some similar type of query) that will give an error??

If yes, how I prevent this from happening?

Thanks?

Important note: I’m using MyISAM! My web hosting don’t allow InnoDB.

  • 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-17T18:26:27+00:00Added an answer on May 17, 2026 at 6:26 pm

    The question is, if two different users votes simultaneously its possible that the two instances of the
    code try to insert a new ID (or some similar type of query) that will give an erro

    Yes, you might end up with two queries doing the insert. Depending on the constraints on the table, one of them will either generate an error, or you’ll end up with two rows in your database.

    You could solve this, I believe, with applying some locking;
    e.g. if you need to add a vote to the product with id theProductId:(pseudo code)

    START TRANSACTION;
    //lock on the row for our product id (assumes the product really exists)
    select 1 from products where id=theProductId for update;
    //assume the vote exist, and increment the no.of votes
    update votes set numberOfVotes = numberOfVotes + 1 where productId=theProductId ;
    //if the last update didn't affect any rows, the row didn't exist
    if(rowsAffected == 0) 
      insert into votes(numberOfVotes,productId) values(1,theProductId )
    //insert the new vote in the per user votes
    insert into user_votes(productId,userId) values(theProductId,theUserId);
    COMMIT;
    

    Some more info here

    MySQL offers another solution as well, that might be applicable here, insert on duplicate

    e.g. you might be able to just do:

     insert into votes(numberOfVotes,productId) values(1,theProductId ) on duplicate key 
       update numberOfVotes = numberOfVotes  + 1;
    

    If your votes table have a unique key on the product id column, the above will
    do an insert if the particular theProductId doesn’t exist, otherwise it will do an update, where it increments the numberOfVotes column by 1

    You could probably avoid a lot of this if you created a row in the votes table at the same time you added the product to the database. That way you could be sure there’s always a row for your product, and just issue an UPDATE on that row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.