dongying6896 2009-08-20 01:38
浏览 57
已采纳

如何编制投票系统?

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 Column is for karma. I have the values of the database table display in an html table, and at the end of each row, I would like a click on a hyperlink, lets say a plus sign, to increase that row's karma level by 1. Then the plus sign would go away.

I should that each row has an auto increment integer as a primary key.

  • 写回答

2条回答 默认 最新

  • dqd3690 2009-08-20 01:51
    关注

    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.";
        }
      }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog