http://hi.baidu.com/devin_he/blog/item/8bbaf273574111198701b090.html
PHP+MySql简单投票系统代码(包括前台、后台)
前台代码:
index.php
<?php include_once("include/conn.php");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="js/public.js"></script>
</head>
<?php
$ObjID="4";
$Sql="select * from tobject where ID='".$ObjID."'";
$Result=mysql_query($Sql);
$Arr=mysql_fetch_array($Result);
$OJBName=$Arr['OJBName'];
$SType=$Arr['SType'];
?>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="616" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="521" class="font1 td1">题目:<?php echo $OJBName?>
<input name="SType" type="hidden" id="SType" value="<?php echo $SType?>" />
<input name="opt" type="hidden" id="opt" />
<input name="ObjID" type="hidden" id="ObjID" value="<?php echo $ObjID?>" /></td>
<td width="80" align="center" class="font2 td1">[<a href="javascript:lookresult('result.php',document.form1,'look');">查看结果</a>]</td>
</tr>
<tr>
<td colspan="2" bgcolor="#FFFFFF" class="font2">
<?php
$Sql="select * from tselect where ObjID='".$ObjID."'";
$Result=mysql_query($Sql);
while($Arr=mysql_fetch_array($Result))
{
if($SType=="0")
{
echo "<input type="radio" name="Sel" value="".$Arr['ID']."" /> ".$Arr['SName']."<br> ";
}
elseif($SType=="1")
{
echo "<input name="SelArr[]" type="checkbox" id="SelArr" value="".$Arr['ID']."" /> ".$Arr['SName']."<br> ";
}
}
?> </td>
</tr>
</table>
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="41" align="center"><input type="button" name="Submit" value="提交" οnclick="lookresult('result.php',document.form1,'vote');" />
<input type="reset" name="Submit2" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
result.php
<?php include_once("include/conn.php");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<?php
$IP=$_SERVER["REMOTE_ADDR"];
$SType=$_POST['SType'];
$Sel=$_POST['Sel'];
$SelArr=$_POST['SelArr'];
$opt=$_POST['opt'];
$ObjID=$_POST['ObjID'];
$Sql="select count(1) as IPNum from IPTable where IP='".$IP."' and ObjID='".$ObjID."'";
$Result=mysql_query($Sql);
$Arr=mysql_fetch_array($Result);
if($opt=="vote")
{
if($Arr['IPNum']<=0)
{
if($SType=="0")
{
$Sql="update tselect set `Num`=`Num`+1 where ID='".$Sel."'";
if(mysql_query($Sql))
{
mysql_query("insert into IPTable (IP,ObjID) values ('".$IP."','".$ObjID."')");
echo "<script>alert('投票成功,感谢您的参与!');</script>";
}
else
{
echo "<script>alert('投票失败,请您重新投票,感谢您的参与!');</script>";
}
}
elseif($SType=="1")
{
$Count=count($SelArr);
for($i=0;$i<$Count;$i++)
{
$SqlStr.=" ID=".$SelArr[$i]." or ";
}
$SqlStr=substr($SqlStr,0,strlen($SqlStr)-4);
$Sql="update tselect set `Num`=`Num`+1 where ".$SqlStr;
if(mysql_query($Sql))
{
mysql_query("insert into IPTable (IP,ObjID) values ('".$IP."','".$ObjID."')");
echo "<script>alert('投票成功,感谢您的参与!');</script>";
}
else
{
echo "<script>alert('投票失败,请您重新投票,感谢您的参与!');</script>";
}
}
}
else
{
echo "<script>alert('您已经投过票,不需要再次投票!');</script>";
}
}
$Sql="select