sql和php数据库条目'落后于'

sql and php database entry 'lagging behind'

本文关键字:php 数据库 sql      更新时间:2023-09-26

我已经实现了一个评级系统,但无法让它在我的一个数据库表中发布正确的最新评级,它总是落后一步,即它总是在当前评级之前存储最后一个评级:

"post_rating"表包含对任何内容的每一次投票的一行,而"post_rat_main"表正是我遇到麻烦的地方。我希望它存储一个由网站上某个项目的所有个人投票组成的总体评分,并将查询保持在最低限度,但正如所说,它总是比当前的运行总数落后一票。

主页

// set up the ratings 
$votes = mysql_query("SELECT rat_rating FROM post_rating WHERE rat_ID = $post_ID"); 
$votesnr = 0; 
$totalvotes = 0; 
while($vote = mysql_fetch_array($votes)){ 
$votesnr++; 
$totalvotes += $vote['rat_rating']; 
} 
if($votesnr == 0){ 
$rating = 0; 
} 
else { 
$rating = $totalvotes/$votesnr; 
} 
$roundedrating = floor($rating) + round($rating - floor($rating)) / 2 ; 
?> 
<div>Total Rating</div> 
<div class="star-rating" id="rating1result<?php echo $roundedrating; ?> "style="background-position:0 -<?php echo $roundedrating * 32; ?>px;"> 
<div class="star"></div> 
<div class="star"></div> 
<div class="star"></div> 
<div class="star"></div> 
<div class="star"></div> 
</div> 
<div class="result"> 
<span style="color:green"><?php echo round($rating,2); ?></span> (<?php echo $votesnr; ?> votes) 
</div> 
<script type="text/javascript"> 
$(function(){ 
$('.star').mouseover(function (){ 
var star = $(this).index()+1; 
var x =(32 * star); 
$(this).parent().css('backgroundPosition... ' +(-x)+ 'px'); 
}); 
$('.star-rating').mouseout(function (){ 
var originalresult = $(this).attr('id').split('result')[1]; 
var y =(32 * originalresult); 
$(this).css('background-position','0%' +(-y)+ 'px'); 
}); 
}); 
$('.star').click(function (){ 
var id = $(this).parent().attr('id').split('ratin... 
var vote = $(this).index() +1; 
$.ajax({ 
type: "POST", 
url:"save_vote.php", 
data: 'id='+ id + '&vote='+ vote + '&stid=<?php echo $post_ID ?>' + '&totv=<?php echo $totalvotes ?>' + '&votes=<?php echo $votesnr ?>' 
}); 
$(this).parent().removeAttr("id"); 
$(this).parent().html(" "); 
window.location.href = "posts/post_view.php?id=<?php echo $post_ID ?>"; 
}); 
</script>` 

这一切都能很好地与"post_rating"表配合使用,我怀疑问题所在的处理程序scrip如下:

$id = intval($_POST['id']); 
$vote = intval($_POST['vote']); 
$post_ID = intval($_POST['stid']); 
$votesnr = $_POST['votes']; 
$tot_v = $_POST['totv']; 
// set up the ratings 
$votesnr = $votesnr; 
$totalvotes = $tot_v; 
if($votesnr == 0){ 
$rating = 0; 
} 
else { 
$rating = $totalvotes/$votesnr; 
} 
$new_rat = round($rating,2); 
// Sling in the data 
$insert_rat = "INSERT INTO post_rating"; 
$insert_rat.= " (rat_ID,";  
$insert_rat.= " rat_rating,";
$insert_rat.= " rat_user_ID,";
$insert_rat.= " rat_user_name,";
$insert_rat.= " rat_date)";
$insert_rat.= " VALUES"; 
$insert_rat.= " ('$post_ID',";
$insert_rat.= " '$vote',"; 
$insert_rat.= " '$member_ID',"; 
$insert_rat.= " '$member_name',";
$insert_rat.= " NOW())";
$story_update = "UPDATE posts_rat_main"; 
$story_update.= " SET rating = '$new_rat'"; 
$story_update.= " WHERE st_ID = '$post_ID'"; 
mysql_query($insert_rat) or die(mysql_error()); 
mysql_query($story_update) or die(mysql_error()); 

因此,每当我更新表"posts_rat_main"时,它都会更新为当前投票后的数字一票!?我从一个教程中得到了一半,并把它搞砸了,但无济于事。我的javascript很糟糕,就是搞不懂,但我怀疑问题出在我的"save_vote"处理程序脚本上,它看起来很简单。

我最近的尝试是从"主页"中截取注释为"//设置评分"的脚本,并将其粘贴到save_vote处理程序脚本中,如图所示,但没有成功。我很困惑,有什么想法吗?

还有一点需要注意的是,我发现这个脚本有时有点反应迟钝,有时需要点击2到3次星星才能登记投票,我无法想象为什么会这样?

非常感谢任何帮助,干杯。

简单的答案是,您必须将$vote添加到$totalvotes,并将+1加入votesnr

$rating = ($totalvotes + $vote) / ($votesnr + 1);

这也将允许您删除if/else,因为分母至少为1。

较长的答案

如果post_rating上有rat_id、rat_rating的索引,那么当您想要结果时,最好只执行按sql分组的select查询。除非你真的有问题,否则像这样的预优化不值得花时间或麻烦。

如果必须有一个单独的表来存储结果,那么至少在post_rating上使用触发器来计算结果,而不是在php中进行计算。