PHP和MySQL投票系统OOP [关闭]

To begin with we're beginners to PHP, we're studying Multimedia Design and we have been assigned to make a website in plain HTML. Furthermore we also have to include some PHP (which must be object-oriented). Our idea is to call the URL from our Youtube videos in our database and each video should have a vote button attached.

We can easily call our videos to a specific page in a div box on our website. This is our video_class.php:

<?php class Video {
private $db;    
public function insertVideo($videoId) {
    $row = $this->db->query("SELECT url FROM video WHERE id = ".$videoId);
    $ost = $this->db->loadRows($row);

    echo '<iframe width="200" height="200" src="https://www.youtube.com/embed/' . $ost[0]['url'] . '" frameborder="0" allowfullscreen></iframe>';
}

public function setDatabaseConnection($db) {
    $this->db = $db;
} } ?>

And the page we're loading it to:

<?php // Create database connection

// Load Database class file
require_once 'db_class.php'; 

//Creating new object instance from Database class
$db = new event(); 

// Run initiate function and provide credentials.
$db->initiate("localhost","root","","event"); 


$db->connect();                 // Connect to MySQL database

// Load Video class file
require_once 'video_class.php'; 


$video  = new Video;
$video->setDatabaseConnection($db);

$row=$db->query("SELECT url FROM video WHERE id = 1");
$ost=$db->loadRows($row);
//var_dump($ost);
$row1=$db->query("SELECT url FROM video WHERE id = 2");
$ost1=$db->loadRows($row1);
//var_dump($ost1);
$row2=$db->query("SELECT url FROM video WHERE id = 3");
$ost2=$db->loadRows($row2);
//var_dump($ost2); ?>

HTML:

    <center><div class="video_clip">

<?php echo '<iframe width="200" height="200" src="https://www.youtube.com/embed/' . $ost[0]['url'] . '" frameborder="0" allowfullscreen></iframe>'; ?>

        <a href="events_vote.php?userid=1&videoid=1"><img src="images/vote.png"></a>

        </div><!--video_clip end-->

But the real problem is next:

We have 3 videos you can vote on by clicking on the vote button, under each video. Each button must count the clicks and store it in our database. We have absolutely no clue how to make this possible. Our teacher told to link to a subpage (for example, "vote.php"). On that page we should use:

  • $_GET[id]
  • fetch id from $get
  • get current votes from video where id = 1/2/3
  • add+1
  • save votes in video where id=1
  • and finish with a redirect

Can someone help us? We have found a few possible solutions on the forums, but still no luck! Sorry for the long post and too much text :)

DATABASE STRUCTURE:

Table name:
users

Table comments: users

Column  Type    Null    Default Comments    MIME

id  int(11) No          
videoId int(11) No          

Table name:
video

Table comments: video

Column  Type    Null    Default Comments    MIME

id  int(11) No          
url varchar(50) No       

This is quite open-ended, so I'll add some clues to get you going.

Firstly, since your votes will affect the database, you should use post and not get (see here for more details)1. Once you've dealt with the operation, you can then do your redirect.

So, under your <iframe>, set up a <form> with a post method. In it, add three input tags each having a type of submit, and each having a different name attribute. For your action, you can aim it at a different page if you want to, but since it is simple I would point it at itself. Thus, use <?php echo $_SERVER[ 'PHP_SELF' ] ?> for the time being.

OK, so this will send the post data to the same page. Thus, in your page PHP, just after your database initialisation, catch the post op like so:

// Your existing code
$db->connect();                 // Connect to MySQL database

// New code
if ($_POST) {
    print_r($_POST);
    exit();
    // @todo Parse the result in your POST array
    // @todo Save the result in the database
    // @todo Redirect to self
}

// Load Video class file
require_once 'video_class.php'; 

What that will do is dump the post data on the screen, and then exit immediately. This is a good prototyping approach to see that you're on the right track.

Adding @todo notes is quite a good approach too - do these in order, and delete the comment when that piece is written and tested. Don't forget to add new comments explaining code, if appropriate.

1 If using the $_GET array is an essential component of the exercise, then you could use three post forms, each with their own button, and with the action containing a separate query string that will appear in the $_GET array. However I'd argue that's a bit convoluted, and probably not the best way to achieve this in practice.

If you want to keep it simple, you might want to skip the part where the page doesn't reload. You can make a button do all sorts of javascript tricks (google jquery and ajax), but there's no need for this.

supposing your url is yourfile.php

  • Make a link called upvote beneath each video linking it to yourfile.php?voteid=xx where xx is the id of the video
  • if you click the link, you get redirected to the same page, but now you have a get parameter
  • In your code, before you show the page, check if any votes are being cast

    if(isset($_GET['voteid']){
        //save vote!
    }
    

Now you are on the same page, you retrieve the votes (one is higher then it was before), and you can just keep on going.