Php 基于会话用户id的五星评级系统
我有一个简单的系统,用于一个大学项目,我被困在其中,非常感谢您的帮助。我已经创建了一个基于用户登录的五星评级系统,我的项目将全部在本地主机上运行,但我需要多人在同一台设备上测试它 我的评分系统运行良好,并将评分插入到我的数据库中,但我希望跨多个页面使用我的评分系统。我有两个文件来完成我的此操作E1index.php和E1ajax.php,我已经包括了这两个文件 我尝试为我的其他页面(E2index.php、E3index.php等)创建单独的索引文件,以便同一用户可以对多个页面进行评级,但每个用户在我的数据库中只插入一个评级,第二个评级将覆盖第一个评级 我的问题是,谁能解释一下我如何修改代码,让我的评分插入到同一用户的单独一行中,这取决于他们所处的页面 E1index.phpPhp 基于会话用户id的五星评级系统,php,mysql,session,rating-system,Php,Mysql,Session,Rating System,我有一个简单的系统,用于一个大学项目,我被困在其中,非常感谢您的帮助。我已经创建了一个基于用户登录的五星评级系统,我的项目将全部在本地主机上运行,但我需要多人在同一台设备上测试它 我的评分系统运行良好,并将评分插入到我的数据库中,但我希望跨多个页面使用我的评分系统。我有两个文件来完成我的此操作E1index.php和E1ajax.php,我已经包括了这两个文件 我尝试为我的其他页面(E2index.php、E3index.php等)创建单独的索引文件,以便同一用户可以对多个页面进行评级,但每个用
<?php
session_start();
include_once 'dbconnect.php';
if( !isset( $_SESSION['user'] ) ) header("Location: index.php");
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array( $res );
$post_id = '1';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-20" />
<title>Welcome - <?php echo $userRow['username']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<div id="left">
<label>NHSCT eNISAT Tutorials</label>
</div>
<div id="right">
<div id="content">
Welcome <?php echo $userRow['forename']; ?> <a href="eNISATVids.php?home">Return to Tutorials page</a> <a href="logout.php?logout">Sign Out</a>
</div>
</div>
<br>
<br>
<br>
<br>
<br>
<p align="center"><img src="title.jpeg" width="400"height="100" alt="title.jpeg">
<br>
<link rel="stylesheet" href="style.css" type="text/css" />
<link type="text/css" rel="stylesheet" href="css/example.css">
<div class="tuto-cnt">
<div class="rate-ex1-cnt">
<div id="1" class="rate-btn-1 rate-btn"></div>
<div id="2" class="rate-btn-2 rate-btn"></div>
<div id="3" class="rate-btn-3 rate-btn"></div>
<div id="4" class="rate-btn-4 rate-btn"></div>
<div id="5" class="rate-btn-5 rate-btn"></div>
</div>
<hr>
<div class="box-result-cnt">
<?php
$query = mysql_query("SELECT * FROM enisat_rate");
while($data = mysql_fetch_assoc($query)){
$rate_db[] = $data;
$sum_rates[] = $data['rate'];
}
if(@count($rate_db)){
$rate_times = count($rate_db);
$sum_rates = array_sum($sum_rates);
$rate_value = $sum_rates/$rate_times;
$rate_bg = (($rate_value)/5)*100;
}else{
$rate_times = 0;
$rate_value = 0;
$rate_bg = 0;
}
?>
<hr>
<h3>This tutorial has been rated <strong><?php echo $rate_times; ?></strong> times.</h3>
<hr>
<h3>The average rating is <strong><?php echo $rate_value; ?></strong> stars.</h3>
<hr>
<div class="rate-result-cnt">
<div class="rate-bg" style="width:<?php echo $rate_bg; ?>%"></div>
<div class="rate-stars"></div>
</div>
<hr>
</div><!-- /rate-result-cnt -->
</div><!-- /tuto-cnt -->
<script>
// rating script
$(function(){
$('.rate-btn').hover(function(){
$('.rate-btn').removeClass('rate-btn-hover');
var therate = $(this).attr('id');
for (var i = therate; i >= 0; i--) {
$('.rate-btn-'+i).addClass('rate-btn-hover');
};
});
$('.rate-btn').click(function(){
var therate = $(this).attr('id');
var dataRate = 'act=rate&post_id=<?php echo $post_id; ?>&rate='+therate; //
$('.rate-btn').removeClass('rate-btn-active');
for (var i = therate; i >= 0; i--) {
$('.rate-btn-'+i).addClass('rate-btn-active');
};
$.ajax({
type : "POST",
url : "http://localhost/Tna/E1ajax.php",
data: dataRate,
success:function(){}
});
});
});
</script>
</body>
</html>
欢迎-
NHSCT eNISAT教程
欢迎
本教程已被评为次。
平均评级为星级。
大学项目真的不应该使用mysql\uuu
数据库扩展,它已被弃用(在PHP7中永远消失),特别是如果你只是在学习PHP,那么就花点精力学习PDO
或mysqli\uu
数据库扩展,我也想不出你为什么要在一个count()
Hi@riggsfully上使用@
错误消音器,你对我的问题有什么建议吗?是的,创建10个不同的用户,每次以不同的用户身份登录并像那样进行测试Hi@riggsfully,我有多个用户,这不是问题。如果我以不同的用户身份登录,他们对某个特定页面的所有投票相加,但如果我创建另一个页面,即不同的评级系统,它仍会记住用户对第一个页面的投票并更新此页面。因此,无论用户在哪个页面上,都只能投票一次。我试图更改每个索引页上的$post_id值,但这不起作用,我的表中的post_id已更改,但仍保留其原始投票权,您能为此提供任何帮助吗?非常感谢,我的项目完成时间不多了
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
if($_POST['act'] == 'rate'){
//search if the userID has already gave a note
$userID=$_SESSION['user'];
$therate = $_POST['rate'];
$thepost = $_POST['post_id'];
$query = mysql_query("SELECT * FROM enisat_rate where user_id= '$userID'");
while($data = mysql_fetch_assoc($query)){
$rate_db[] = $data;
}
if(@count($rate_db) == 0 ){
mysql_query("INSERT INTO enisat_rate (id_post, user_id, rate)VALUES('$thepost', '$userID', '$therate')");
}else{
mysql_query("UPDATE enisat_rate SET rate= '$therate' WHERE user_id = '$userID'");
}
}
?>