评级脚本限制通过ip投票

时间:2017-03-20 20:11:02

标签: mysql sql

我试图限制ip可以在数据库中投票的次数,但我确实遇到了问题

我的两张桌子概述

T1(评级)

id          votes       rating      category     rating
----------- ---------- ----------- ----------- -----------

T2(rating_users)

user_id     user_ip     user_vote  user_source
----------- ---------- ----------- -----------

表1 - > ID 与...有关系 表2 - > USER_SOURCE

过去两天我尝试了很多不同的查询,但我无法弄清楚如何做到这一点:(

现在我使用此查询:

select *
from rating
left join rating_users on rating_users.user_source = rating.id
where rating_users.user_ip is null
    or rating_users.user_ip != $ip
order by RAND() LIMIT 1

让我们假设table1有5行数据..

如果ip1进入网站并且表2为空(truncatet),并且他对所有5张图像进行投票,那么“第6张”图像将不会是我想要的......

但是如果ip2进入并且表2仍然拥有来自ip1的投票,那么ip2可以按照他的意愿投票,因为没有一行是“NULL”......

我真的迷失在这里,自周六/周日晚上我试图解决这个问题。

有人可以帮助我,我在这里得到了很多白发:(

编辑我的代码 - >

Forside.php(index.php)

<?php
$dbCon = dbCon();

$sql = "SELECT * FROM rating ORDER BY id DESC LIMIT 1";
$result = $dbCon->query($sql);
$result = $result->fetch_object();

$id = $result->id;

//$tal = ip2long($_SERVER['REMOTE_ADDR']);
//$tal = long2ip($_SERVER['REMOTE_ADDR']);
$ip = ip2long($_SERVER['REMOTE_ADDR']);

// CHECKKER OM DEN IP HAR VOTET DETTE BILLEDE
$sql = "SELECT user_ip, user_source, user_vote FROM rating_users WHERE user_ip = $ip AND user_source = $id";
$result = $dbCon->query($sql);
$result = $result->fetch_object();
$rating = $result->user_vote;


if (!empty($result)) {
//    echo 'Du har allerede votet dette billede : ' . $rating . "/5";
    //IMGINFO
    $string = "<div class=\"flex absolute centerAm borc1 boxsb\">";
    $string = $string .= "<div id=\"innerVotingWrapper\" class=\"innerVotingWrapper relative flex \">";

    $string = $string .= "<div id=\"imgInfo\" class=\"imgInfo absolute top0 right0\">";
    $string = $string .= "<div id=\"imgRefresh\">";
    $string = $string .= "</div>";
    $string = $string .= "</div>";

    $string = $string .= "<div class=\"imageWrapper mauto relative fadeInClass\">";
    $string = $string .= "<img id=\"imgSrc\" src=\"assets/img/$id.png\" class=\"babeImg borb2 boxsb\">";
    $string = $string .= "<div id=\"starWrapper\" class=\"flex absolute left0 bot0 mar10\">";

    for ($i = 1; $i <= $rating; $i++) {
        $string = $string .= "<div class='voteFull'></div>";
    }
    for ($i = $rating; $i < 5; $i++) {
        $string = $string .= "<div class='voteEmpty'></div>";
    }
    $string = $string .= "</div>"; //STAR WRAPPER
    $string = $string .= "</div>"; //IMAGE WRAPPER
    $string = $string .= "</div>";//INNER WRAPPER
    $string = $string .= "</div>";//YDRE WRAPPER

//HVIS PERSONEN IKKE HAR STEMT FØR
} else {


    $string = "<div class=\"flex absolute centerAm borc1 boxsb\">";
    $string = $string .= "<div id=\"innerVotingWrapper\" class=\"innerVotingWrapper relative flex \">";
    //IMGINFO
    $string = $string .= "<div id=\"imgInfo\" class=\"imgInfo absolute top0 right0\">";
    $string = $string .= "</div>";

    $string .= "<div class=\"imageWrapper mauto relative fadeInClass\">";

    $string .= "<img id=\"imgSrc\" src=\"assets/img/$id.png\" class=\"babeImg borb2 boxsb\">";

    //BILLEDE VALUEN
    $string .= "<input id=\"imgValue\" class=\"absolute displayn\" type=\"radio\" value=\"$id\">";

    //STAR FORM
    $string .= "<div id=\"starWrapper\" class=\"starWrapper flex center absolute left0 bot0 mar10\">";
    $string .= "<form action=\"\" method=\"post\" class=\"flex flex-drr absolute bot0 left0\">";

    for ($i = 5; $i > 0; $i--) {
        $string .= "<input id=\"vote$i\" class=\"vote displayn\" type=\"radio\" name=\"vote\" value=\"$i\">";
        $string .= "<label for=\"vote$i\"></label>";
    }

    //SUBMIT FORM
    $string .= "<input type=\"submit\" id=\"voteSubmit\" class=\"displayn\">";

    $string .= "</form>";
    $string .= "</div>"; //starWrapper
    $string .= "</div>"; //imageWrapper
    $string .= "</div>"; //innerVotingWrapper
    $string .= "</div>"; //YDRE NAV TIL AT CENTRERE
}
//COL10
$test = "<div id=\"testid\" class=\"bgw\">";
$test .= "</div>";
?>

<div class="flex">
    <div class="col10 bgc2">
        <?php echo $test; ?>
    </div>
    <div class="col90 bgc1 relative vh100">
        <?php echo $string; ?>
    </div>
</div>

newimage.php

<?php

require_once '../includes/db.php';
require_once '../includes/functions.php';
$dbCon = dbCon();

//$voteValue = filter_input(INPUT_POST, 'voteValue', FILTER_SANITIZE_NUMBER_INT);
//$imgValue = filter_input(INPUT_POST, 'imgValue', FILTER_SANITIZE_NUMBER_INT);

$voteValue = $_POST['voteValue'];
$imgValue = $_POST['imgValue'];

$ip = ip2long($_SERVER['REMOTE_ADDR']);

$stmtt = $dbCon->prepare("INSERT INTO rating_users (user_ip, user_vote, user_source) VALUES (?, ?, ?)");
$stmtt->bind_param('iii', $ip, $voteValue, $imgValue);
$stmtt->execute();

//UPDATERE DATABASED
$stmt = $dbCon->prepare("UPDATE rating SET
    votes = votes + 1,
    rating = rating + ? WHERE id = ?");
$stmt->bind_param('ii', $voteValue, $imgValue);
$stmt->execute();


//LAV EN SQL QUERY SOM CHECKKER AT id ikke matcher et id allerede votet af den ip
//SENDER NY QUERY AFSTED

$sql = "SELECT * FROM rating INNER JOIN rating_users ON rating_users.user_source = rating.id WHERE rating_users.user_source= $user_source AND (rating_users.user_id= $user_id OR rating_user.ip= $ip ) ";
$result = $dbCon->query($sql);
$result = $result->fetch_object();
$id = $result->id;
//HVIS DEN IKKE ER TOM SKAL DET BILLEDE + RATING DIVS SENDES SOM JSON
$imgSrc = "assets/img/" . $id . ".png";
$arr = array('imgSrc' => $imgSrc, 'id' => $id);

echo json_encode($arr);

的Javascript / AJAX

var vote = document.getElementsByClassName('vote');
var voteL = vote.length;
for (let i = 0; i < voteL; i++) {
    //SKAL VÆRE LET VALUE
    let voteValue = vote[i].value;

    vote[i].addEventListener('click', function () {
        var imgValue = document.getElementById("imgValue").value;
        newImage(voteValue, imgValue);
        console.log(vote[i].value);
    });
}

function newImage(voteValue, imgValue) {
    var http = new XMLHttpRequest();
    var url = "pages/newImage.php";
    var params = "voteValue=" + voteValue + "&imgValue=" + imgValue;
    http.open("POST", url, true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.onreadystatechange = function () { //KALDER FUNKTIONEN NÅR DNE ER KLAR
        if (http.readyState == 4 && http.status == 200) {

            var Data = JSON.parse(this.responseText);
            console.log(this.responseText);

            var imgSrc = document.getElementById('imgSrc');
            imgSrc.src = Data.imgSrc;
            var imgValue = document.getElementById('imgValue');
            imgValue.value = Data.id;

            var ratingValue = Data.rating;
            var starWrapper = document.getElementById('starWrapper');
            //HVIS IPEN HAR STEMT PÅ BILLEDET BLIVER HANS VOTES RETURNERET TIL VISNING OG IKKE MULIGT AT STEMME
            if (Data.rating) {

                // OPRETTER VOTED/EMPTY STARS HVIS DER ER VOTET
                var string = "";
                for (i = 1; i <= ratingValue; i++) {
                    string = string + "<div class=\"voteFull\"></div>";
                }
                for (i = ratingValue; i < 5; i++) {
                    string = string + "<div class=\"voteEmpty\"></div>";
                }
                starWrapper.innerHTML = string;

            } else {
                console.log('Data.rating er ikke sat');
            }

        }
    };
    http.send(params);
}

var testId = document.getElementById('testid');
testId.addEventListener('click', function () {
    console.log('Clicked');
});

1 个答案:

答案 0 :(得分:0)

这是数据1的要求,你应该对列user_source上的t2有唯一约束,因为用户只能投票一次,另外你可以将ip添加到唯一约束限制每个IP /用户一票

SELECT *
FROM rating
INNER JOIN rating_users 
    ON rating_users.user_source = rating.id
WHERE rating_users.user_source= $user_source
AND (rating_users.user_id= $user_id OR rating_user.ip= $ip ) 

如果用户或ip尚未为当前照片投票,则上述查询应返回零

编辑:我将查询更改为限制为user_id / user_ip并将其分段为当前照片(user_source)。这意味着您必须将以下内容传递给查询$user_source$user_id$ip