Voting Script, Possiblity of Simplifying Database Queries

Posted by Sev on Stack Overflow See other posts from Stack Overflow or by Sev
Published on 2010-05-28T23:47:13Z Indexed on 2010/05/28 23:52 UTC
Read the original article Hit count: 168

Filed under:
|
|

I have a voting script which stores the post_id and the user_id in a table, to determine whether a particular user has already voted on a post and disallow them in the future.

To do that, I am doing the following 3 queries.

SELECT user_id, post_id from votes_table where postid=? AND user_id=?

If that returns no rows, then:

UPDATE post_table set votecount = votecount-1 where post_id = ?

Then

SELECT votecount from post where post_id=?

To display the new votecount on the web page

Any better way to do this? 3 queries are seriously slowing down the user's voting experience

© Stack Overflow or respective owner

Related posts about php

Related posts about sql