douwa6220 2017-03-14 16:26
浏览 20
已采纳

评级脚本在多种费率时不会更新

Hi i am currently making a rating script and i have a few problems

Right now the script "kinda" works -> http://xch07.wi2.sde.dk/sandbox/rating2/index.php

The problem is that if i open up 2 browsers and load image 1 on both browsers and rate the image on both the browsers the last query sent will override anything inbetween so basically in the end only 1 vote out of the 2 is submitted?. Right now my db looks like this: id - votes - rating

Currently i just increment the votes with 1 when a vote is submitted And i raise the raiting with whatever value has ben voted

Could someone tell what i have to do to overcove this problem? and any other thoughs on my code are greatly appreciated :)

OBS: Does anyone have an idea of how i can check if a person has already voted a given image?

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);
}

THE PAGE AJAX REQUESTS

<?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);
  • 写回答

3条回答 默认 最新

  • doudaochu1699 2017-03-14 16:34
    关注

    Why don't you replace the prepared statement with the following:

    $stmt = $dbCon->prepare("UPDATE rating SET
        votes = ?,
        rating = rating + ? WHERE id = " . $id);
    

    Held og lykke :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dsyk33753 2017-03-14 16:30
    关注

    Just amend your query to add 1 to what is currently in the column like this

    $stmt = $dbCon->prepare("UPDATE rating 
                                SET votes = votes + 1,
                                    rating = rating + ?
                            WHERE id = ?");
    $stmt->bind_param('ii', $_POST['voteValue'], $_POST['id']);
    
    评论
  • dsq30861 2017-03-14 16:38
    关注

    By submitting the current rating/votes to your PHP script, you open yourself up to potentially updating your database based on stale information. You can UPDATE values in the database based on current row values, so the following would work as well:

    $stmt = $dbCon->prepare("UPDATE rating SET votes = votes + 1, rating = rating + ? WHERE id = ?");
    $stmt->bind_param('ii', $_POST['voteValue'], $_POST['id']);
    $stmt->execute();
    
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
  • ¥15 要求编写稀疏矩阵A的转置矩阵的算法
  • ¥15 编写满足以下要求的停车场管理程序,设停车场只有一个可停放n辆车的狭窄通道且只有一个大门可供车辆进出。
  • ¥20 powerbulider 导入excel文件,显示不完整
  • ¥15 用keil调试程序保证结果进行led相关闪烁
  • ¥15 paddle训练自己的数据loss降不下去
  • ¥20 用matlab的pdetool解决以下三个问题
  • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?
  • ¥15 嵌入式Linux固件,能直接告诉我crc32校验的区域在哪不,内核的校验我已经找到了,uboot没有
  • ¥20 h3c静态路要求有详细过程