0
I have a screen that is sending to table emails through $_POST
. After that goes to a voting screen.
I want to make sure that the email that has already been sent cannot vote again, but I’m not sure how to do that.
This is my page that has the form.
<?php
if(!empty($_POST['cadastra'])){
if(!empty($_POST['email'])){
$email = $_POST['email'];
$ip = $_SERVER['REMOTE_ADDR'];
$pegaEmail = mysql_query("SELECT * FROM email WHERE email = '$email'");
if(mysql_num_rows($pegaEmail) < 1){
if(mysql_query("INSERT INTO email (email, ip) VALUES ('$email','$ip')")){
echo '<script>window.location="http://enquetegoias.esy.es/pesquisa.html"</script>';
}else{
echo '<script>alert("Erro ao prosseguir! '.mysql_error().'") </script>';
}
}else{
echo '<script>window.location="http://enquetegoias.esy.es/pesquisa.html"</script>';
}
}else{
echo "<script>alert('Preencha todos os campos!')</script>";
}
}
?>
<body>
<div id="container-pesquisa" >
<div id="login">
<form action="" id="formLogin" method="POST">
<div class="logo">
<img src="img/logo.jpg">
</div>
<div class="titulo-login"><h1>Eleições OAB-GO 2015</h1></div>
<table>
<tbody>
<p style="text-align: center;">Para votar ou acompanhar esta votação entre com seu e-mail</p>
<p style="font-size: 12px; text-align: center; font-style: italic">(Só será validado um voto por e-mail).</p>
<tr>
<td><input type="email" name="email" class="lgnSenha" id="lgnSenha" placeholder="E-mail" required></td>
</tr>
<tr>
<input name="cadastra" type="hidden" value="1">
<td colspan="2"><input type="submit" name="btnLogin" class="btnLogin" id="btnLogin" value="Próximo" ></td>
</tr>
<script type="text/javascript">
</script>
</tbody>
</table>
<div style="margin-left: 180px; margin-bottom: 10px;" class="fb-share-button" data-href="<?="http://".$server.$endereco;?>" data-layout="button_count"></div>
<p style="font-size: 12px; text-align: center">Os números gerados em nossa enquete não têm valor científico. <br />O objetivo é apenas promover o debate e discussão sobre os possíveis<br /> nomes divulgados na imprensa goiana. </p>
</form>
</div>
</div>
</body>
</html>
E essa a página que tem a enquete:
<?php
ob_start (); // Inicia o buffer de saída
?>
<?php
//Informações do banco de dados de atualização de acordo com as configurações do servidor
if(@!$_POST['poll'] || !$_POST['pollid']){
$query=mysql_query("SELECT id, ques, created_on FROM questions ORDER BY id DESC LIMIT 1");
while($row=mysql_fetch_assoc($query)){
//Questões
//echo "<p class=\"pollques-data\" >Criada em: ".date("d/m/Y H:i", strtotime($row['created_on']))."</p>";
echo "<p style='text-align: justify;' class=\"pollques\" >".$row['ques']."</p>";
//echo "<p class=\"pollques-computado\" >Verifique o resultado abaixo:</p>";
$poll_id=$row['id'];
}
if(@$_GET["result"]==1 || @$_COOKIE["voted".$poll_id]=='yes'){
//Se já votou ou pediu resultado
echo '<p style="font-size: 10px; text-align: center;">Seu voto já foi computado com sucesso. Agradecemos a sua participação!</p>';
showresults($poll_id);
exit;
}
else{
//Opções
$query=mysql_query("SELECT id, value, image FROM options WHERE ques_id=$poll_id");
if(mysql_num_rows($query)){
echo '<div id="formcontainer" ><form method="post" id="pollform" action="'.$_SERVER['PHP_SELF'].'" >';
echo '<input type="hidden" name="pollid" value="'.$poll_id.'" />';
while($row=mysql_fetch_assoc($query)){
echo "<img style='margin-bottom: -22px;' class='presidente' style='background-color: red;' src='image/".$row['image']."' width='60'/></td>";
echo '<input style="margin-bottom: -50px; margin-left: 10px;" type="radio" name="poll" value="'.$row['id'].'" id="option-'.$row['id'].'" />
<label for="option-'.$row['id'].'" >'.$row['value'].'</label><br /><br />'; //aqui
}
echo '<p><input style="cursor: pointer; float: left;padding: 8px 0; width: 100px; margin-right: 25px; background-color: #b7372e; border: 1px dashed #C76767; color: #fff; margin-top:20px;" type="submit" value="Votar" /></p></form>';
echo '<p style="margin-top:20px;"><a href="'.$_SERVER['PHP_SELF'].'?result=1" id="viewresult">Ver Resultados</a></p></div>';
}
}
}
else{
echo '<script>alert("Seu voto foi computado com sucesso. Agradecemos a sua participação!") </script>';
if(@$_COOKIE["voted".$_POST['pollid']]!='yes'){
//Verifica se a opção já foi selecionada no banco
$query=mysql_query("SELECT * FROM options WHERE id='".intval($_POST["poll"])."'");
if(mysql_num_rows($query)){
$query="INSERT INTO votes(option_id, voted_on, ip) VALUES('".$_POST["poll"]."', '".date('Y-m-d H:i:s')."', '".$_SERVER['REMOTE_ADDR']."')";
if(mysql_query($query))
{
//Voto adicionado à base de dados
setcookie("voted".$_POST['pollid'], 'yes', time()+86400*300);
}
else
echo "Ocorreu um erro: ".mysql_error();
}
}
showresults(intval($_POST['pollid']));
}
function showresults($poll_id){
global $conn;
$query=mysql_query("SELECT COUNT(*) as totalvotes FROM votes WHERE option_id IN(SELECT id FROM options WHERE ques_id='$poll_id')");
while($row=mysql_fetch_assoc($query))
$total=$row['totalvotes'];
$query=mysql_query("SELECT options.id, options.value, COUNT(*) as votes FROM votes, options WHERE votes.option_id=options.id AND votes.option_id IN(SELECT id FROM options WHERE ques_id='$poll_id') GROUP BY votes.option_id ORDER BY votes DESC");
while($row=mysql_fetch_assoc($query)){
$percent=round(($row['votes']*100)/$total);
echo '<div class="option" ><p>'.$row['value'].' (<em>'.$percent.'%, '.$row['votes'].' voto(s)</em>)</p>';
echo '<div class="bar ';
if($_POST['poll']==$row['id']) echo ' yourvote';
echo '" style="width: '.$percent.'%; " ></div></div>';
}
echo '<p>Total de Votos: '.$total.'</p>';
echo '<a class="botao" href="index.php" id="viewresult">Voltar</a>';
include '../ajax-poll/facebook.php';
}
ob_end_flush (); // Finaliza buffer de saída
What have you tried to do? Show what you have already done and better specify your question so we can help you.
– Jorge B.
You can do this using, Session, cookie, database, etc... depends on the requirements for application
– Jefferson Silva
I updated the question, @Jorgeb.
– Fernanda Ferreira
Managed to elaborate @Fernandaferreira ?
– Sr. André Baill
No, @Andrébaill, I’m extremely sleepy on this case.
– Fernanda Ferreira
Okay, let’s try to help.
– Sr. André Baill
But you’re passing the email by post, at the time of voting, do you enter it? I saw that made the check if it is < 1, he can vote, if not, you print the error on the screen?
– Sr. André Baill
This first screen that has the form is there just to get the email, after I get the email and go to the voting screen, @Andrébaill. You can chat here?
– Fernanda Ferreira
Let’s go continue this discussion in chat.
– Sr. André Baill