如何允许一名选民投票给多名候选人,但又不允许他再次投票给同一位候选人?

时间:2015-12-20 05:46:19

标签: php mysql

亲爱的朋友,我需要快速回复我正在建立一个教师评级系统。学生评价他们的老师。我需要一个学生给所有老师打分,但不要评价那个学生被评为一次的老师。问题在于我的代码是一名学生只评价一位老师所以我该怎么办?

candidates.php

<?php
	if(isset($_SESSION['ALREADY'])){
	echo '<div style="background-color:#ffebe8; border:1px solid #dd3c10; padding:5px; color:#000; border-radius: 0px; font-family:tahoma; font-size:12px; margin-right:10px;">';
	echo $_SESSION['ALREADY']; 
	unset($_SESSION['ALREADY']);
	echo '</div>';
}?>
<?php
	if(isset($_SESSION['SAVED'])){
	echo '<div style="background-color:#abd46e; border:1px solid #518413; padding:5px; color:#000; border-radius: 0px; font-family:tahoma; font-size:12px;margin-right:10px;">';
	echo $_SESSION['SAVED']; 
	unset($_SESSION['SAVED']);
	echo '</div>';
}?>
<?php
mysql_connect("localhost","root","") or die ("couldnt connnect to server");
mysql_select_db("project")  or die ("couldnt connnect to database");
$find_data=mysql_query("select * from teacher");
while($row=mysql_fetch_assoc($find_data))
{
$id=$row['tid'];
$name=$row['tname'];
$sub=$row['subject'];
$current_rating=$row['rating'];
$hits=$row['hits'];
echo"
<form action='submit-votes.php' method='post'>
$name:<select name='rating'>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>

</select>
<input type='hidden' value='$sub' name='subject'>
<input type='submit' value='Rate'>Current Rating"; echo round($current_rating,2); echo"
</form>
";


}?>



submit-votes.php

<?php
	//session
	session_start();

	//databse connection
	include_once 'config.php';

	//Connect to mysql server
	$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	if(!$link) {
		die('Failed to connect to server: ' . mysql_error());
	}
	
	//Select database
	$db = mysql_select_db(DB_DATABASE);
	if(!$db) {
		die("Unable to select database");
	}
	
		//Function to sanitize values received from the form. Prevents SQL injection
	function clean($str) {
		$str = @trim($str);
		if(get_magic_quotes_gpc()) {
			$str = stripslashes($str);
		}
		return mysql_real_escape_string($str);
	}
	
		if($_SESSION['SESS_VOTERS'] != ''){
		$qry = "SELECT * FROM votes WHERE voters='$_SESSION[SESS_VOTERS]'";
		$result = mysql_query($qry);
		if($result) {
			if(mysql_num_rows($result) > 0) {
				$errmsg_arr = '<i>You already submitted your votes. Please <a href="logout.php" style="color:#004e49;"><u>Logout.</u></a></i>';
				$_SESSION['ALREADY'] = $errmsg_arr;
				$errflag = true;
				session_write_close();
				header('location: candidates.php');
		exit();
			}
			@mysql_free_result($result);
		}
		else {
			die("Query failed");
		}
	}


	//student votes
	$sql=("INSERT INTO votes (voters) VALUES ('$_SESSION[SESS_VOTERS]')");
	
$sub=$_POST['subject'];
$post_rating=$_POST['rating'];
$find_data=mysql_query("select * from teacher where subject='$sub'");
while($row=mysql_fetch_assoc($find_data))
{
$id=$row['tid'];
$current_rating=$row['rating'];
$current_hits=$row['hits'];
}
$new_hits=$current_hits +1;
$update_hits=mysql_query("update teacher set hits='$new_hits' where tid='$id'");

$pre_rating=$current_rating+$post_rating;
$new_rating=$pre_rating/2;

$update_rating=mysql_query("update teacher set rating='$new_rating' where tid='$id'");
	if (!mysql_query($sql,$link))
  	{
  	die('Error: ' . mysql_error());
  	}
  	//show a message query excecuted.
	$saved ='<i>You have successfully submitted your votes. Thank you for voting.</i>';
	$_SESSION['SAVED'] = $saved;
	session_write_close();
	header("location: candidates.php");
	mysql_close($link);
?> 

0 个答案:

没有答案