gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:49:28 25 4
gpt4 key购买 nike

亲爱的 friend 们,我需要快速回复,我正在创建一个教师评分系统。学生对他们的老师进行评分。我需要一个学生对所有老师进行评分,但不要对已被该学生评分过一次的老师进行评分。问题在于我的代码是一名学生仅对一名老师进行评分,那么我该怎么办?

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

最佳答案

要更改的部分是:

    $qry = "SELECT * FROM votes WHERE voters='$_SESSION[SESS_VOTERS]'";

您的代码仅验证学生 ID。您还必须使用 AND 更改验证教师 ID 的请求。

关于php - 如何允许一名选民投票给多名候选人,但不允许他再次投票给同一候选人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34378357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com