2

我刚开始自学php。我正在给自己一些微型项目来推动自己。

到目前为止,我有一个通过 php 表单创建的 MYSQL 数据库。一列是为了业力。我将数据库表的值显示在 html 表中,在每一行的末尾,我想单击一个超链接,比如说一个加号,以将该行的业力级别增加 1。然后加号会消失。

我应该每行都有一个自动增量整数作为主键。

4

2 回答 2

12

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.";
    }
  }

}
于 2009-08-20T01:51:19.460 回答
2

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.

于 2009-08-20T02:02:42.003 回答