Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá amigos, preciso liberar a minha enquete para que as pessoas podem votar varias vezes ao mesmo tempo... como faço isso? Segue o código abaixo.
Ajudem por favor!!!
Arquivo enquete.php
<?
include_once("global.php");
$conn = mysql_connect($host,$userDB,$pwdDB);
mysql_select_db($dataBase);
if ($id_enquete == "") {
$sql="Select min(id_enquete) as id_max from $tablename";
$query = mysql_query($sql,$conn);
$result = mysql_fetch_array($query);
$id_enquete = $result["id_max"];
mysql_free_result($query);
}
$sql = "SELECT * FROM $tablename WHERE id_enquete =$id_enquete";
$query = mysql_query($sql,$conn);
$result = mysql_fetch_array($query);
?>
<script language="javascript"> function res_enquete(op) {
id_p = document.enquete.id_p.value;
id_r = document.enquete.id_r.value;
url="resultados.php?todo="+op+"&id_p="+id_p+"&id_r="+id_r;
ResultWin = window.open(url,"winresult","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,re
sizable=no,left=0,top=0,width=300,height=300");
ResultWin.focus();
}
function voto(id) {
document.enquete.id_r.value = id;
// res_enquete('votar');
}
function votar_enquete() {
if (document.enquete.id_r.value == "")
alert("Por favor, selecione uma opção !");
else
res_enquete('votar'); }
-->
</script>?>
<link href="css/css.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/JavaScript">function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}</script>
<script language="JavaScript" type="text/JavaScript">function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);</script>
<table border="0" cellspacing="1" width="160">
<tr>
<td height="25" colspan="2" align="center"> <font size=2 color=white><b><i>ENQUETE</i></b></font></td>
</tr>
<tr bgcolor="">
<td align="center" colspan="2">
<form name="enquete" method="post" onsubmit="return votar()">
<input type="hidden" name="id_p" value="<?= $id_enquete ?>">
<input type="hidden" name="id_r" value="">
<b><?= $result["texto"]; ?></b>
<?
// Seleciono todas as resposta por ordem crescente de entrada '
$sql ="SELECT * FROM $tablename WHERE id_parente=$id_enquete ORDER BY id_enquete";
$query = mysql_query($sql,$conn);
$total = mysql_num_rows($query);
// pega o total de perguntas
$counter = 0;
while ($counter < $total) {
$id_r = mysql_result($query, $counter, "id_enquete");
$resposta = mysql_result($query, $counter, "texto");
// Cria um conjunto de radio buttons com o valor do ID da resposta e o texto da resposta '
?>
<tr>
<td width="10%" align="center">
<input selected type="radio" name="resposta" value="<?= $id_r ?>" onClick="voto(<?= $id_r ?>);">
</td>
<td width="90%">
<?= $resposta ?>
</td>
</tr>
<?
$counter = $counter + 1;
} ?>
<tr>
<td align="center" colspan="2" height="25" valign="middle"> <a href="java script: votar_enquete()">
<img src="images/votar.gif" width="37" height="13" border="0" valign="middle"></a>
<a href="java script: res_enquete('')"> <img src="images/resultado.gif" width="71" height="13" border="0" valign="middle"></a>
</tr>
</form>
</table>mysql_free_result($query);
mysql_close($conn);
?>______________________________________
Arquivo resultado.php
<?
include_once("global.php");
$id_r = $HTTP_GET_VARS['id_r'];
$id_p = $HTTP_GET_VARS['id_p'];
$todo = $HTTP_GET_VARS['todo'];
$enq = "enq$id_p";
$msg = "";
$conn = mysql_connect($host,$userDB,$pwdDB);
if (!$conn) {
die ("Falha de Conexão!"); }
mysql_select_db($dataBase);
if ($todo=="votar") {
// PHP adiciona um hit á resposta da enquete
// recupera os dados dos campos hidden
if (!$_COOKIE[$enq]) {
$remotaddr = $_SERVER["REMOTE_ADDR"];
$query=mysql_query("update $tablename set hits= hits + 1 where id_enquete=$id_r",$conn);
setcookie ("$enq", $remotaddr,time()+86400);
$msg = "Seu voto foi computado com sucesso!";
}
else {
$msg="Você já votou nesta Enquete hoje !";
}
}
$sql="SELECT * FROM $tablename WHERE id_enquete=$id_p";
$query = mysql_query($sql, $conn);
$result = mysql_fetch_array($query);
$pergunta = $result["texto"];
mysql_free_result($query);
?>
<html>
<head>
<TITLE>LAB-Enquete</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../style.css" type="text/css">
</head>
<body bgcolor="#FFFFFF" topmargin=0 leftmargin=0>
<table width="100%" border="0" align=center>
<?
$sql="SELECT * FROM $tablename WHERE id_parente=$id_p";
$query = mysql_query($sql, $conn);
$totreg = mysql_num_rows($query);
$sql = "select sum(hits) as total from $tablename where id_parente=$id_p";
$query2 = mysql_query($sql, $conn);
$total = mysql_result($query2, 0, "total");
mysql_free_result($query2);
?>
<tr>
<td colspan="3" align="center" class="titulo" width="100%">
<b><?= $pergunta ?></b></td>
</tr>
<tr>
<td colspan="3" align="center"><font color=green><b> <? echo $msg ?></b></font></td>
</tr>
<tr><? $counter = 0;
while ($counter < $totreg) {
$resposta = mysql_result($query, $counter, "texto");
$hits = mysql_result($query, $counter, "hits");
?>
<tr>
<td colspan="3" width="100%">
<img src="images/percent.gif" border="0" height="1" width="100%"></td>
</tr>
<tr>
<td width="150"><?= $resposta ?></td>
<? if ($total > 0) { ?>
<td width="100" class="boxborder"><img src="images/percent.gif" border="0" height="11" width="<?= sprintf("%.2f",100/$total * $hits)?>"></td>
<td width="50" align="right"><?= sprintf("%.2f",100/$total * $hits) ?>%<b></td>
<?} else {?>
<td width="100" class="boxborder"><img src="images/percent.gif" border="0" height="11" width=""></td>
<td width="50" align="right">0%<b></td>
<? } ?>
</tr>
<tr><?
// Passo á resposta seguinte
$counter=$counter + 1;
} ?>
<tr>
<td colspan="2" align="right" class="total" width="250">Total de Votos</td>
<td width="50" class="total" align="right"><b><?= $total ?></b></td>
</tr>
<tr>
<td colspan="3" align="center" class="total"><a href="java script: self.close();"><br>:: Fechar ::</a></td>
</tr>
</table>
</body>
</html>
<?
mysql_free_result($query);
mysql_close($conn);
?>____________________________________________________Arquivo home.php
<?
$conn = mysql_connect($host,$userDB,$pwdDB);
mysql_select_db($dataBase) or die("Database não localizada!!");
$pagesize = 3;
if ($pagetogo == "") {
$pagetogo = 0; }
$sql="select * from $tablename where id_parente=0 order by data desc";
$query = mysql_query($sql, $conn);
if (!$query) { ?>
<script>
document.location.href="install.php";
</script><? }
$maxRec = mysql_num_rows($query);
$maxPage = $maxRec / $pagesize;
$maxPage = (int)$maxPage;
if ($maxRec == $maxPage * $pagesize)
$maxPage = $maxPage - 1;
?>
<table width="80%" bgcolor="#336699" align=center class=boxborder cellspacing="2" cellpadding="2">
<tr>
<td colspan=5 align=center bgcolor="#FF9900"><font size="3"><b>Administração
de Enquetes | <a href="login.php?logout=1">Sair</a></b></font></td>
<td align="center"><a href="java script: history.back();"><img src="images/voltar.gif" border=0 alt="Voltar"></a><!-- linha 29 - fechar tag <a> --></td>
</tr>
<tr bgcolor="#000050"><font color="#ffffff">
<td class=titulo>ID</td>
<td class=titulo>Data</td>
<td class=titulo>Pergunta</td>
<td class=titulo>Resp</td>
<td class=titulo>Editar</td>
<td class=titulo>Apagar</td></font><!-- fechar tag <font> da linha 31-->
</tr><?
if (!$maxRec) { ?>
<tr class=grey2>
<td colspan=6 align=center bgcolor="#FF9900">Banco de dados vazio</td>
</tr>
<tr>
<td class=center colspan=6>
<a href="index.php?todo=add">Adicionar Enquete</a></td>
</tr>
<?
# Se tiver perguntas no banco de dados, exibe as perguntas e suas respostas
}
else {
$counter = 0;
$offset = ($pagetogo) * $pagesize;
mysql_data_seek($query, $offset);
While ($counter < $pagesize && $counter+$offset < $maxRec) {
$pergunta = mysql_fetch_array($query);
$data = substr($pergunta['data'], 0, strlen($pergunta['data'])-6);
$ano = substr($data,0, 4);
$mes = substr($data,4, 2);
$dia = substr($data,6, 2);
$fdata = "$dia/$mes/$ano";
$sql = "select count(*) as total from $tablename where id_parente=" . $pergunta['id_enquete'];
$total = mysql_query($sql, $conn);
$t = mysql_result($total, 0, 'total');
?>
<tr bgcolor=#efefef>
<td class=dir><?= $pergunta['id_enquete'] ?></td>
<td class=dir><?= $fdata ?></td>
<?
if ($todo=="edt" && $id_p==$pergunta['id_enquete']) { ?>
<form name="edit" action="#" onsubmit="return validate(this);">
<input type="hidden" name="id" value="<?= $id_p ?>">
<input type="hidden" name="pagetogo" value="<?= $pagetogo ?>">
<td colspan="2"><input type=text name=texto size=50 value="<?= $pergunta['texto'] ?>"></td>
<td class=center>
<input type=submit value="Alterar" name="edit" class="but"></td></form><? }
else { ?>
<td><b><?= $pergunta['texto'] ?></b></td>
<td>
<a href="index.php?todo=add&id=<?= $pergunta['id_enquete'] ?>">
<img src="images/botao_adicionar.gif" border=0 alt="Adicionar Resposta"> (<?= $t ?>)</a></td>
<td class=center>
<a href="index.php?todo=edt&id_p=<?= $pergunta['id_enquete'] ?>&pagetogo=<?= $pagetogo ?>">
<img src="images/botao_editar.gif" border=0></a></td>
<? } ?>
<td class=center>
<a href="index.php?todo=del&id_p=<?= $pergunta['id_enquete'] ?>" OnClick="return confirm('Deseja excluir?')">
<img src="images/botao_apagar.gif" border=0></a></td>
</tr><?
$counter ++;
$query2 = mysql_query("select * from $tablename where id_parente=" . $pergunta['id_enquete'], $conn);
$x = 0;
while ($result = mysql_fetch_array($query2)) {
$x=$x+1; ?>
<tr bgcolor=#ffffff>
<td colspan="2" class=dir>Resposta # <?= $x ?></td>
<? if ($todo!="edt" || $id!=$result['id_enquete']) { ?>
<td colspan="2"><b><?= $result['texto'] ?></b></td>
<td class=center>
<a href="index.php?todo=edt&id=<?= $result['id_enquete'] ?>&pagetogo=<?= $pagetogo ?>">
<img src="images/botao_editar.gif" border=0></a></td><? }
else { ?>
<form name="edit" action="#" onsubmit="return validate(this);">
<input type="hidden" name="id" value="<?= $id ?>">
<input type="hidden" name="pagetogo" value="<?= $pagetogo ?>">
<td colspan="2"><input type=text name=texto size=50 value="<?= $result['texto'] ?>"></td>
<td class=center>
<input type=submit value="Alterar" name="edit" class="but"></td>
</form><?
} ?>
<td class=center>
<a href="index.php?todo=del&id=<?= $result['id_enquete'] ?>" OnClick="return confirm('Deseja excluir?')">
<img src="images/botao_apagar.gif" border=0></a></td>
</tr>
<?
} #while 2
// echo "<tr><td colspan=6><hr size=1></td></tr>";
// die();
} #while 1 ?><tr>
<td colspan="6">
<table width="100%" bgcolor="#ffffff" align="center">
<tr>
<td class=center>
<a href="index.php?todo=add">Adicionar Enquete</a></td>
<?
echo "<form action=\"index.php\" method=\"POST\">\n";
echo "<td valign=middle width=\"25%\">";
echo "<select name=pagetogo size=1 class=combo>";
for ($x=0; $x<$maxPage+1; $x++) {
$page=$x+1;
echo "<option value=$x";
if ($pagetogo==$x)
echo " selected ";
echo ">Página $page</option>"; }
echo"</select> ";
echo "<input type=submit name=submit value=\" ir \" class=but></td>";
echo "</form>";
?></table><!-- fechar tag <table> da linha 128 -->
</td>
</tr>
</table></td>
</tr> <?
} # else
mysql_free_result($query);
mysql_close($conn); ?>
</table>
<script language="javascript">
function validate(form) {
if (form.texto.value =='') {
alert("Preencha o campo!");
form.texto.focus();
return false;
}
return true;
}
</script>Carregando comentários...