How to program a vote up system?

前端 未结 2 766
情歌与酒
情歌与酒 2021-02-11 02:55

I am in the very beginnings of teaching myself php. I am giving myself micro projects to push myself.

Thus far I have a MYSQL database, created through a php form. One C

相关标签:
2条回答
  • 2021-02-11 03:21

    Personally I like Jonathan's answer.

    However if you feel like you might need more info, I might be able to help.

    As a small side project I tried to make a quote database like bash.org for my university.
    It was developed using MySql and PHP, it has posting/voting much like what you are trying to accomplish.
    It is by no means a well designed web app. However it might be able to get you going in the right direction.

    Live testing site: link (be gentle!)

    Code on GitHub: link

    I would take a look at the database schema , the php-db-integration and the ajax to update a vote.

    The code is fairly simple and straight foreword. One thing of note is the "filter_input"s, These functions are from a PHP library to sanitize user inputs to prevent SQL injections.

    0 讨论(0)
  • 2021-02-11 03:23

    For this example, let's assume you're voting on so-answers. This will require at least three tables:

    Users, Answers, Votes

    The votes table will hold all the history:

    voteid | userid | answerid | value
    ----------------------------------
       1   |   12   |   383    |   1
       2   |   28   |   383    |  -1  (negative number would require signed values)
    

    In this example, we see that two votes have been recorded. Users 12 and 28 both voted on Answer 383. User 12 loved it, and added 1 to its support. User 28 didn't like it, and subtracted 1 from its support.

    Anytime a user votes, you should first check to see if that user has already voted on that particular question. If they have, you can reject any further attempts to vote, or overwrite their old vote with a new one.

    SELECT * 
    FROM votes 
    WHERE (userid = 12) 
      AND (answerid = 383)
    

    That's a very simple example query that will tell you if the user has already voted or not. If it returns records, you know they've voted. You can respond with a very nice "Sorry, you've already voted." message, or you can overwrite it:

    UPDATE votes 
    SET value = $votevalue 
    WHERE (userid = 12) 
      AND (answerid = 383)
    

    Having said that, let's look at how SO accomplishes this:

    When you click the up-vote arrow, SO fires a request off to a url like the following:

        http://stackoverflow.com/posts/1303528/vote/2

    In this URL, we can see the vote is being cast on post #1303528. And the vote-type is represented by 2. This 2 likely represents "add one." You could use a more basic url, and go with something like:

        vote.php?answerid=383&vote=1

    The vote.php page would have code similar to the following:

    (warning: do not use this code as-is. It is meant to be an example, not a solution)

    if ($_GET) {
    
      $userid   = $_SESSION["userid"]; // assumes the user is logged in via SESSIONS
      $answerid = $_GET["answerid"];
      $votetype = $_GET["vote"];
    
      $query = "SELECT * 
                FROM votes 
                WHERE (userid = {$userid}) 
                  AND (answerid = {$answerid})";
    
      $result = mysql_query($query) or die(mysql_error());
    
      if (mysql_num_rows($result) > 0) {
        # User has voted
        print "Sorry, you are only allowed one vote.";
      } else {
        # User has not voted, cast the vote
        $query = "INSERT INTO votes (userid, answerid, votevalue)
                  VALUES({$userid},{$answerid},{$vote})";
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_affected_rows($result) > 0) {
          print "Your vote has been recorded.";
        }
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题