我正在开发一个提供评级的简单Web应用程序。用户只能使用其IP地址对ONCE进行投票。然而,问题在于有多种评价因素,但每当用户将评分输入一个字段时,他就无法将评级输入其他评级。我希望我的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等。
答案 0 :(得分:0)
如果你可以评价多件事,你应该更新你的tbl_rating,至少添加一列,然后执行select并插入该列(让我们说是rated_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";
}
}
}
然后将fieldset 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的方法。