gpt4 book ai didi

php - 允许用户在 PHP 中投票一次

转载 作者:太空宇宙 更新时间:2023-11-03 12:28:25 26 4
gpt4 key购买 nike

我正在做一个学校项目,我需要允许用户对图片进行投票。用户可以将图片向上或向下。他们可以随时更改投票,但不能撤消投票,因此一旦他们投票,要么赞成,要么反对。

我一直在尝试一些事情,但我似乎无法让它发挥作用。它在用户第一次按下赞成票时起作用,然后用户可以将他的投票改为反对票。但是当他再次尝试投票时,什么也没有发生,这已经困扰我一段时间了,我将不胜感激任何帮助。

到目前为止,这是我的代码:

 if (isset($_SESSION['loggedin'])) {
$result = mysql_query("SELECT * FROM user2pics WHERE picid = $id AND userid = $user");
if (mysql_num_rows($result) == 0) {
$votes_up = $cur_votes[0] + 1;
$resultaat = mysql_query("UPDATE pics SET likes = $votes_up WHERE picid = $id");
if ($resultaat) {
$query = mysql_query("INSERT INTO user2pics (picid, userid, vote) VALUES ($id, $user, 1)");
if ($query) {
$effectiveVote = getEffectiveVotes($id);
echo $effectiveVote . " votes";
} elseif (!$query) {
echo "Failed!";
}
} elseif (!$resultaat) {
echo "Failed insert in pics!";
}
} else {
$row = mysql_fetch_array($result);
if ($row['vote'] == 0) {
$votes_down = $cur_votes[0] + 1;
$result = mysql_query("UPDATE pics SET likes = $votes_up WHERE picid = $id");
if ($result) {
$resultaat = $mysqli -> prepare("UPDATE user2pics SET vote = 1 WHERE picid = $id AND userid = $user");
$resultaat -> execute();
$effectiveVote = getEffectiveVotes($id);
if ($resultaat -> affected_rows == 1) {
echo $effectiveVote . " votes";
}
}
} else {
$effectiveVote = getEffectiveVotes($id);
echo $effectiveVote . " votes";
}
}
} else {
echo "Please login first!";
}
} elseif ($action == 'vote_down'){
if (isset($_SESSION['loggedin'])) {
$result = mysql_query("SELECT * FROM user2pics WHERE picid = $id AND userid = $user");
if (mysql_num_rows($result) == 0) {
$votes_down = $cur_votes[1] + 1;
$resultaat = mysql_query("UPDATE pics SET dislikes = $votes_down WHERE picid = $id");
if ($resultaat) {
$query = mysql_query("INSERT INTO user2pics (picid, userid, vote) VALUES ($id, $user, 0)");
if ($query) {
$effectiveVote = getEffectiveVotes($id);
echo $effectiveVote . " votes";
} elseif (!$query) {
echo "Failed to dislike!";
}
} elseif (!$resultaat) {
echo "Failed insert in pics!";
}
} else {
$row = mysql_fetch_array($result);
if ($row['vote'] == 1) {
$votes_down = $cur_votes[1] + 1;
$result = mysql_query("UPDATE pics SET dislikes = $votes_down WHERE picid = $id");
if ($result) {
$resultaat = $mysqli -> prepare("UPDATE user2pics SET vote = 0 WHERE picid = $id AND userid = $user");
$resultaat -> execute();
$effectiveVote = getEffectiveVotes($id);
if ($resultaat -> affected_rows == 1) {
echo $effectiveVote . " votes";
}
}
} else {
$effectiveVote = getEffectiveVotes($id);
echo $effectiveVote . " votes";
}
}
} else {
echo "Please login first!";
}
}

$cur_votes 定义为:$cur_votes = getAllVotes($id);

function getAllVotes($id) {
$votes = array();
$q = "SELECT * FROM pics WHERE picid = $id";
$r = mysql_query($q);
if (mysql_num_rows($r) == 1)//id found in the table
{
$row = mysql_fetch_assoc($r);
$votes[0] = $row['likes'];
$votes[1] = $row['dislikes'];
}
return $votes;
}

function getEffectiveVotes($id) {
/**
Returns an integer
**/
$votes = getAllVotes($id);
$effectiveVote = $votes[0] - $votes[1];
return $effectiveVote;
}

最佳答案

您通过在两个地方存储“喜欢”来复制功能。

我没有查看您的弱实体(用户和投票表),所以我们假设它将包含三个字段:user_iditem_idvote TINYINTuser_iditem_id 上的主键,因此同一用户只能对每个项目投一票。

根据赞成或反对将投票设置为 1 或 -1,而不是将 likes 存储在项目表中,像这样动态计算项目的总票数:

SELECT SUM(vote) FROM user_votes WHERE item_id = ?;

如果您只想要赞成票,请执行以下操作:

SELECT SUM(vote) FROM user_votes WHERE item_id = ? AND vote = 1;

当用户想要记录或更改他的投票时,您可以使用 REPLACE INTO 语法(感谢 Anigel 的建议——我完全错过了)来存储用户的新投票:

REPLACE INTO user_votes (user_id, item_id, vote) VALUES (?, ?, ?);

关于php - 允许用户在 PHP 中投票一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16588672/

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