您好我正在制作评级脚本,我有一些问题
现在剧本"有点"作品 - > http://xch07.wi2.sde.dk/sandbox/rating2/index.php
问题在于,如果我在两个浏览器上打开2个浏览器并加载图像1并对两个浏览器上的图像进行评级,则发送的最后一个查询将覆盖其间的任何内容,因此基本上最终只提交了1个投票?。 现在我的数据库看起来像这样: id - 投票 - 评级
目前,我只是在提交投票时将投票数增加1 而且我用任何已投票的价值提高了评价
有人可以告诉我要做些什么来解决这个问题吗?我的代码中的任何其他内容都非常感谢:)
OBS:有没有人知道我如何检查一个人是否已经投票给定图像?
HTML
<div class="flex">
<div class="imageWrapper mauto relative fadeInClass">
<img id="imgSrc" src="assets/img/<?php echo $id ?>.png" class="carImg">
<input id="imgValue" class="absolute displayn" type="radio" value="<?php echo $id ?>">
<input id="imgVotes" class="absolute displayn" type="radio" value="<?php echo $votes ?>">
<input id="imgRating" class="absolute displayn" type="radio" value="<?php echo $rating ?>">
<form action="" method="post" class="flex flex-drr absolute bot0 left0">
<input id="vote5" class="vote displayn" type="radio" name="vote" value="5">
<label for="vote5"></label>
<input id="vote4" class="vote displayn" type="radio" name="vote" value="4">
<label for="vote4"></label>
<input id="vote3" class="vote displayn" type="radio" name="vote" value="3">
<label for="vote3"></label>
<input id="vote2" class="vote displayn" type="radio" name="vote" value="2">
<label for="vote2"></label>
<input id="vote1" class="vote displayn" type="radio" name="vote" value="1">
<label for="vote1"></label>
<input type="submit" id="voteSubmit" class="displayn">
</form>
</div>
</div>
的Javascript / AJAX
var vote = document.getElementsByClassName('vote');
var voteL = vote.length;
for (let i = 0; i < voteL; i++) {
vote[i].addEventListener('click', function () {
let imgValue = document.getElementById("imgValue");
let imgVotes = document.getElementById("imgVotes");
let imgRating = document.getElementById("imgRating");
let imgValueVal = imgValue.value;
let imgVotesVal = imgVotes.value;
let imgRatingVal = imgRating.value;
let voteValue = vote[i].value;
newImage(imgValueVal, imgVotesVal, imgRatingVal, voteValue);
});
}
function newImage(id, votes, rating, voteValue) {
var http = new XMLHttpRequest();
var url = "pages/newImage.php";
var params = "id=" + id + "&votes=" + votes + "&rating=" + rating + "&voteValue=" + voteValue;
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function () {//Call a function when the state changes.
if (http.readyState == 4 && http.status == 200) {
alert(http.responseText);
var Data = JSON.parse(this.responseText);
alert(Data.id);
let imgSrc = document.getElementById('imgSrc');
imgSrc.src = Data.imgSrc;
let imgValue = document.getElementById('imgValue');
imgValue.value = Data.id;
let imgVotes = document.getElementById('imgVotes');
imgVotes.value = Data.votes;
console.log(Data.votes);
let imgRating = document.getElementById('imgRating');
imgRating.value = Data.rating;
}
}
http.send(params);
}
页面AJAX请求
<?php
require_once '../includes/db.php';
require_once '../includes/functions.php';
$dbCon = dbCon();
//UPDATERE DATABASED
$id = $_POST['id'];
$votes = $_POST['votes'];
$rating = $_POST['rating'];
$voteValue = $_POST['voteValue'];
$votes++;
$rating = $rating + $voteValue;
$stmt = $dbCon->prepare("UPDATE rating SET
votes = ?,
rating = ? WHERE id = " . $id);
$stmt->bind_param('ii', $votes, $rating);
$stmt->execute();
//SENDER NY QUERY AFSTED
define("SQL", "SELECT * FROM rating ORDER BY rand() LIMIT 1");
$result = $dbCon->query(SQL);
$result = $result->fetch_object();
$id = $result->id;
$votes = $result->votes;
$rating = $result->rating;
$imgSrc = "assets/img/" . $id . ".png";
$arr = array('imgSrc' => $imgSrc, 'id' => $id, 'votes' => $votes, 'rating' => $rating);
echo json_encode($arr);
答案 0 :(得分:1)
为什么不用以下内容替换预先准备好的声明:
$stmt = $dbCon->prepare("UPDATE rating SET
votes = ?,
rating = rating + ? WHERE id = " . $id);
Held og lykke: - )
答案 1 :(得分:0)
只需修改您的查询,将1添加到当前列中的内容
$stmt = $dbCon->prepare("UPDATE rating
SET votes = votes + 1,
rating = rating + ?
WHERE id = ?");
$stmt->bind_param('ii', $_POST['voteValue'], $_POST['id']);
答案 2 :(得分:0)
通过向PHP脚本提交当前评级/投票,您可以根据陈旧信息自行更新数据库。您可以根据当前行值更新数据库中的值,因此以下内容也可以使用:
addGadget