【问题标题】:Multiple ratings using Jquery with single IP address使用具有单个 IP 地址的 Jquery 进行多个评级
【发布时间】:2017-12-13 08:23:12
【问题描述】:

我正在开发一个提供评级的简单网络应用程序。用户只能使用其 IP 地址投票一次。然而,问题是有多个要评分的东西,但是每当用户在一个字段中输入他的评分时,他就不能在其他评分中输入评分。我希望我的 PHP 文件知道我只想为一个评分验证 IP 地址,而用户可以对其他内容进行评分。

这是我的 JQuery 代码:

<h1>Demo 1</h1>
<script>
    $(document).ready(function () {
        $("#demo1 .stars").click(function () {
            var id = 1;

            $.post('rating.php', {rate:$(this).val()}, function(d) {
                if (d>0) {
                    alert('You already rated');
                } else {
                    alert('Thanks For Rating');
                }
            });

            $(this).attr("checked");
        });
    });
</script>
<fieldset id='demo1' class="rating">
    <input class="stars" type="radio" id="star10" name="rating" value="10" />
    <label class = "full" for="star10" title="Very nice - 10/10"></label>
    <input class="stars" type="radio" id="star9" name="rating" value="9" />
    <label class = "full" for="star9" title="Great - 9/10"></label>
    <input class="stars" type="radio" id="star8" name="rating" value="8" />
    <label class = "full" for="star8" title="Great - 8/10"></label>
    <input class="stars" type="radio" id="star7" name="rating" value="7" />
    <label class = "full" for="star7" title="Good - 7/10"></label>
    <input class="stars" type="radio" id="star6" name="rating" value="6" />
    <label class = "full" for="star6" title="Good - 6/10"></label>
    <input class="stars" type="radio" id="star5" name="rating" value="5" />
    <label class = "full" for="star5" title="Meh - 5/10"></label>
    <input class="stars" type="radio" id="star4" name="rating" value="4" />
    <label class = "full" for="star4" title="Meh - 4/10"></label>
    <input class="stars" type="radio" id="star3" name="rating" value="3" />
    <label class = "full" for="star3" title="Sucks - 3/10"></label>
    <input class="stars" type="radio" id="star2" name="rating" value="2" />
    <label class = "full" for="star2" title="Sucks - 2/10"></label>
    <input class="stars" type="radio" id="star1" name="rating" value="1" />
    <label class = "full" for="star1" title="Sucks - 1/10"></label>
</fieldset>

id = demo2, demo3 等有多个这样的字段集。我想要每个演示的唯一标识符或变量。

这就是我的 PHP 处理请求并将其发送到数据库的方式:

   if (isset($_POST['rate']) && !empty($_POST['rate'])) {

    $rate = $conn->real_escape_string($_POST['rate']);
    $sql = "SELECT `id` FROM `tbl_rating` WHERE `user_id`='" . $ipaddress . "'";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();

    if ($result->num_rows > 0) {
        echo $row['id'];
    } else {
        $sql = "INSERT INTO `tbl_rating` ( `rate`, `user_id`) VALUES ('" . $rate . "', '" . $ipaddress . "'); ";
        if (mysqli_query($conn, $sql)) {
        echo "0";
        }
    }
}

我只能想到一种解决方案,它可以在点击时触发一个标志,1 代表 demo1,2 代表 demo2,3 代表 demo3,等等。

【问题讨论】:

  • 使用 IP 作为标识符通常是一种不好的方法,因为通常有很多人在一个 IP 后面(公司/家庭/任何开放的 wifi 等)。无论如何,您需要有一个评级表,其中包含对象(该人正在评级的内容)、评级和标识符。然后,您只需检查此人是否对特定对象进行了评分。
  • 嘿马格努斯,感谢您的评论。如果我不希望人们投票两次,我可以使用其他验证器吗?我也不希望他们创建新帐户。另外,我同意一个 IP 地址后面的人投票一次。我只希望使用该 IP 地址对每个字段集进行投票。现在,我的 PHP 使用它来检查用户是否已经投票:$sql = "SELECT id` FROM tbl_rating WHERE user_id='" . $ipaddress . "'";` @MagnusEriksson
  • 如果你想阻止人们多次评分,你需要一个合适的标识符,而匿名用户根本无法做到这一点。您将需要创建一个帐户(并通过电子邮件或类似方式验证)或使用 facebook connect。这两种方法都不是防弹的,但通常足够好。
  • 您可能会接受它,但您的用户会接受吗?
  • 我刚刚检查了您编辑的评论,这就是我想做的,但我不知道该怎么做。

标签: javascript php jquery ajax mysqli


【解决方案1】:

如果您可以对不止一件事进行评分,则应更新您的 tbl_rating 添加至少一列,然后使用该列执行选择和插入(假设为 rating_field)。

if (isset($_POST['rate']) && !empty($_POST['rate']) && isset($_POST['rateField']) && !empty($_POST['rateField'])) {
    $rate = $conn->real_escape_string($_POST['rate']);
    $rateField = $conn->real_escape_string($_POST['rateField']);
    $sql = "SELECT `id` FROM `tbl_rating` WHERE `user_id` = '" . $ipaddress . "' AND `rated_field` = '" . $ratedField . "' LIMIT 1";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        echo $row['id'];
    } else {
        $sql = "INSERT INTO `tbl_rating` ( `rate`, `rated_field`, `user_id`) VALUES ('" . $rate . "', '" . $ratedField . "', '" . $ipaddress . "'); ";
        if (mysqli_query($conn, $sql)) {
            echo "0";
        }
    }
}

然后将字段集 id 作为 rate_field 值发送:

$(document).ready(function () {
    $(".stars").click(function () {
        var id = 1;

        $.post('rating.php', {rate:$(this).val(), rateField:$(this).parent().attr('id')}, function(d) {
            if (d>0) {
                alert('You already rated');
            } else {
                alert('Thanks For Rating');
            }
        });

        $(this).attr("checked");
    });
});

它会起作用的。

另外,考虑使用try to get the real ip的方法。

【讨论】:

  • 请同时添加limit 1。你正在寻找 0 或 1,然后使用 limit 1 除非你有一个该死的好理由
  • 嘿,@jeprubio 这正是我的想法,但我不知道如何为每个单独的 FieldSet 获取我的 $ratedField。你能帮忙吗?
  • INSERT 语句中有多余的双引号
  • @AakashPhadtare 您可以使用您的字段集 ID,只需使用 jQuery 将其添加到您的 $.post 中。我刚刚用这个编辑了答案。
  • 在大型数据库中,有索引,这仍然为我节省了大量时间(哈哈以毫秒计,但仍然如此)。你只需要检查它是否存在,不要过度选择。此外,如果您知道如何使用索引,它们也很棒,但由于这是一个入门级问题,我提供入门级建议。
猜你喜欢
  • 2016-12-01
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
相关资源
最近更新 更多