我有一个upvote按钮和一个downvote按钮,我正在尝试使用cookie来防止同一个用户在投票后进行投票,但是我的代码让他们在灰化投票按钮之前完全投票两次。我的代码出了什么问题?
<html>
<form action="" method="post">
<input type="hidden" name="action" value="submit"/>
<input id="upvote" type="submit" name="submit" value="Upvote"
<?php
if (isset($_COOKIE['chocolatechip']) and $_COOKIE['chocolatechip'] =
"voted=1")
{
echo "disabled";
}
?>>
<input id="downvote" type="submit" name="submit" value="Downvote"
<?php
if (isset($_COOKIE['chocolatechip']) and $_COOKIE['chocolatechip'] =
"voted=1")
{
echo "disabled";
}
?>>
</form>
</html>
<?php
if (isset($_POST['action']))
{
if ($_POST['submit'] == "Upvote") // if upvoted clicked
{
mysqli_query($conn,"UPDATE object_votes_table SET upvotes = upvotes + 1
WHERE object = 'Ceramic'");
setcookie('chocolatechip', 'voted=1');
}
if ($_POST['submit'] == "Downvote") // if downvoted clicked
{
mysqli_query($conn,"UPDATE object_votes_table SET downvotes = downvotes
+ 1 WHERE object = 'Ceramic'");
setcookie('chocolatechip', 'voted=1');
}
}
?>
答案 0 :(得分:0)
尝试
... and $_COOKIE['chocolatechip'] == "voted=1"
而不是
... and $_COOKIE['chocolatechip'] = "voted=1"
我没有解释为什么你的代码在第二次之后工作,但希望这会有所帮助......