目前我正在尝试为我的网站创建投票功能。现在,投票功能正在运行,但一旦我刷新我的页面,它就会回到默认值。我可以知道如何解决这个问题。
这是我的PHP代码:
<?php
$db = getConnection();
$sql = "SELECT * FROM photos ";
$result = $db->prepare($sql);
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
?>
<div class="item" data-postid="<?php echo $row['photoID'] ?>"
data-score="<?php echo $row['vote'] ?>"> <!-- Thus line shows the row id and the vote score -->
<div class="vote-span"><!-- voting-->
<div class="vote" data-action="up" title="Vote up">
<i class="icon-chevron-up"></i>
</div><!--vote up-->
<div class="vote-score"><?php echo $row['vote'] ?></div>
<div class="vote" data-action="down" title="Vote down">
<i class="icon-chevron-down"></i>
</div><!--vote down-->
</div>
<?php
//echo $row['eventName'];
$photoPath = $row['photoPath'];
echo "<div class='row'>
<a href='$photoPath' data-toggle='lightbox'>
<img class='img-thumbnail' src='$photoPath' alt='' width='300' height='300'>
</a>
</div>
</div>";
}
?>
这是ajax代码:
if($_SERVER['HTTP_X_REQUESTED_WITH']){
if(isset($_POST['postid']) AND isset($_POST['action'])){
$postid = $_POST['postid'];
$db = getConnection();
$db->quote($postid);
if(isset($_SESSION['vote'][$postid])) return;
$sql="SELECT * FROM photos WHERE photoID ='{$postid}'
LIMIT 1";
$result = $db->prepare($sql);
$result->execute();
if($rows = $result->fetch(PDO::FETCH_ASSOC)){
if($_POST['action'] === 'up'){
$vote = ++$rows['vote'];
}else{
$vote = --$rows['vote'];
}
$sqlupdate = "UPDATE photos SET vote = '{$vote}'
WHERE photoID = '{$postid}'";
$result = $db->prepare($sqlupdate);
$result->execute();
$_SESSION['vote'][$postid] = true;
}
}
}