现在的位置: 首页 > 综合 > 正文

PHP+MySql简单投票系统代码

2013年08月12日 ⁄ 综合 ⁄ 共 18140字 ⁄ 字号 评论关闭

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="提交" onclick="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 * 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="739" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
      <td colspan="3" class="font1 td1">题目:<?php echo $OJBName?></td>
    </tr>
    <tr>
      <td align="center" bgcolor="#FFFFFF" class="font2">选项</td>
      <td align="center" bgcolor="#FFFFFF" class="font2">比例</td>
      <td align="center" bgcolor="#FFFFFF" class="font2">票数</td>
    </tr>
<?php
//计算总票数
$Sql="select sum(`Num`) as VoteCount from tselect where ObjID='".$ObjID."'";
$Result=mysql_query($Sql);
$Arr=mysql_fetch_array($Result);
$VoteCount=$Arr['VoteCount'];
$Sql="select * from tselect where ObjID='".$ObjID."'";
$Result=mysql_query($Sql);
while($Arr=mysql_fetch_array($Result))
   {
   $Percent=round(($Arr['Num']/$VoteCount)*100,2);
   $Width=round($Percent*4,0);
?>
    <tr>
      <td width="214" bgcolor="#FFFFFF" class="font2"><?php echo $Arr['SName']?></td>
      <td width="405" bgcolor="#FFFFFF" class="font2"><img src="images/per.gif" width="<?php echo $Width?>" height="20" align="absmiddle" /> (<?php echo

$Percent."%"?>)</td>
      <td width="98" align="center" bgcolor="#FFFFFF" class="font2"><?php echo $Arr['Num']?></td>
    </tr>
<?
   }
?>
</table>
<table width="739" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td width="739" height="41" align="center"><input type="button" name="Submit" value="返回" onclick="window.location.href='index.php'" />
      </td>
    </tr>
</table>
</form>
</body>
</html>

public.js

function selectAll(obj) {
for(var i = 0;i<obj.elements.length;i++)
if(obj.elements[i].type == "checkbox")
obj.elements[i].checked = true;
}

function selectOther(obj) {
for(var i = 0;i<obj.elements.length;i++)
if(obj.elements[i].type == "checkbox" ) {
   if(!obj.elements[i].checked)
    obj.elements[i].checked = true;
   else
    obj.elements[i].checked = false;
}
}

function objedit(page,f,id)
{
f.opt.value="edit";
f.ID.value=id;
f.action=page;
f.submit();
}

function Objadd(page,f)
{
f.opt.value="add";
f.action=page;
f.submit();
}

function objdel(page,f,id)
{
if(confirm("您确认要删除这个项目吗?"))
{
f.opt.value="del";
f.ID.value=id
f.action=page;
f.submit();
}
}

function Objdelsel(page,f)
{
if(confirm("您确认要删除这些项目吗?"))
{
f.opt.value="delsel";
f.action=page;
f.submit();
}
}

function objselect(page,f,id)
{
f.opt.value="list";
f.ObjID.value=id;
f.action=page;
f.submit();
}

function lookresult(page,f,opt)
{
f.opt.value=opt;
f.action=page;
f.submit();
}

conn.php

<?php
$Host="localhost";
$UserName="root";
$Password="111";
$DB="toupiao";
@mysql_connect($Host,$UserName,$Password) or die("连接数据库服务器失败,请与系统管理员联系");
@mysql_select_db($DB) or die("连接数据库失败,请与系统管理员联系");
?>

style.css

.font1{ font-family:"宋体"; font-size:12px; line-height:23px; font-weight:bold;}
.font2{ font-family:"宋体"; font-size:12px; line-height:23px;}
.font3{ font-family:"宋体"; font-size:12px;}
.td1{ border:1px solid #FFF;}

后台代码:

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>

<body>

<table width="862" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <form id="form1" name="form1" method="post" action="">
<tr>
      <td width="20" align="center" class="font1 td1"> </td>
      <td width="68" align="center" class="font1 td1">编号</td>
      <td width="169" align="center" class="font1 td1">类别</td>
      <td width="409" align="center" class="font1 td1">
     <input name="opt" type="hidden" id="opt" />
        <input name="ID" type="hidden" id="ID" />
        <input name="ObjID" type="hidden" id="ObjID" />    
        项目</td>
      <td width="160" align="center" class="font1 td1">操作</td>
    </tr>
<?php
$Sql="select * from tobject order by ID asc";
$Result=mysql_query($Sql);
while($Arr=@mysql_fetch_array($Result))
   {
?>
    <tr>
      <td bgcolor="#FFFFFF" class="font2"><input name="IDArr[]" type="checkbox" id="IDArr[]" value="<?php echo $Arr['ID']?>" /></td>
      <td align="center" bgcolor="#FFFFFF" class="font2"><?php echo $Arr['ID']?></td>
      <td align="center" bgcolor="#FFFFFF" class="font2"><?php echo ($Arr['SType']=="0")?"单选":"多选"?></td>
      <td align="left" bgcolor="#FFFFFF" class="font2"><?php echo $Arr['OJBName']?></td>
      <td align="center" bgcolor="#FFFFFF" class="font2">[<a href="javascript:objedit('objopt.php',document.form1,<?php echo $Arr['ID']?>)">编辑</a>][<a

href="javascript:objdel('objopt.php',document.form1,<?php echo $Arr['ID']?>)">删除</a>][<a href="javascript:objselect('select.php',document.form1,<?php echo

$Arr['ID']?>)">选项</a>]</td>
    </tr>
    <?
   }
?>
<tr>
      <td colspan="3" align="center" bgcolor="#FFFFFF" class="font2">[<a href="javascript:selectAll(document.form1);">全选</a>][<a

href="javascript:selectOther(document.form1);">反选</a>][<a href="javascript:Objadd('objopt.php',document.form1);">增加</a>][<a href="javascript:Objdelsel

('objopt.php',document.form1);">删除</a>]</td>
      <td colspan="2" align="center" bgcolor="#FFFFFF" class="font2"> </td>
      </tr>
</form>
</table>

</body>
</html>
objopt.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
$opt=$_POST['opt'];
$ID=$_POST['ID'];
$IDArr=$_POST['IDArr'];
switch ($opt)
{
case "add":
    $OptName="添加新项目";
$OptStr="int";
break;
case "int":
    $Sql="insert into tobject (OJBName,SType) values('".$_POST['OJBName']."','".$_POST['SType']."')";
if(@mysql_query($Sql))
   {
   echo "<script>alert('添加成功!');window.location.href='index.php';</script>";
   exit;
   }
else
   {
   echo "<script>alert('添加失败!');window.location.href='index.php';</script>";
   exit;
   }
break;
case "edit":
    $OptName="编辑项目";
$OptStr="upd";
$Sql="select * from tobject where ID='".$ID."'";
$Result=@mysql_query($Sql);
$Arr=@mysql_fetch_array($Result);
$OJBName=$Arr['OJBName'];
$SType=$Arr['SType'];
break;
case "upd":
    $Sql="update tobject set OJBName='".$_POST['OJBName']."',SType='".$_POST['SType']."' where ID='".$ID."'";
if(@mysql_query($Sql))
   {
   echo "<script>alert('编辑成功!');window.location.href='index.php';</script>";
   exit;
   }
else
   {
   echo "<script>alert('编辑失败!');window.location.href='index.php';</script>";
   exit;
   }
break;
case "del":
    $Sql="delete from tobject where ID='".$ID."'";
if(@mysql_query($Sql))
   {
   echo "<script>alert('删除成功!');window.location.href='index.php';</script>";
   exit;
   }
else
   {
   echo "<script>alert('删除失败!');window.location.href='index.php';</script>";
   exit;
   }
break;
case "delsel":
    $Count=count($IDArr);
if($Count>0)
   {
   $SqlStr="";
   for($i=0;$i<$Count;$i++)
     {
     $SqlStr.=" ID=".$IDArr[$i]." or ";
     }
   $SqlStr=substr($SqlStr,0,strlen($SqlStr)-4);
   $Sql="delete from tobject where ".$SqlStr;
   if(@mysql_query($Sql))
     {
     echo "<script>alert('删除成功!');window.location.href='index.php';</script>";
     exit;
     }
   else
     {
     echo "<script>alert('删除失败!');window.location.href='index.php';</script>";
     exit;
     }
   }
else
   {
   echo "<script>alert('提交参数错误,没有选择任何要删除的项目!');window.location.href='index.php';</script>";
   }
break;
}
?>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="862" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
      <td colspan="2" align="left" class="font1"> <?php echo $OptName?></td>
    </tr>
    <tr>
      <td width="242" align="right" bgcolor="#FFFFFF" class="font1">项目名称:</td>
      <td width="605" align="left" bgcolor="#FFFFFF" class="font2"><input name="OJBName" type="text" class="font3" id="OJBName" value="<?php echo $OJBName?>"

size="70" /></td>
    </tr>
    <tr>
      <td align="right" bgcolor="#FFFFFF" class="font1">项目类型:</td>
      <td align="left" bgcolor="#FFFFFF" class="font2"><input name="SType" type="radio" value="0" <?php if($SType=="0" || trim($SType)=="") echo "checked";?>

/>
        单选
          <input type="radio" name="SType" value="1" <?php if($SType=="1") echo "checked";?> />
      多选
      <input name="opt" type="hidden" id="opt" value="<?php echo $OptStr?>" />
      <input name="ID" type="hidden" id="ID" value="<?php echo $ID?>" /></td>
    </tr>
</table>
<table width="862" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td height="38" align="center"><input type="submit" name="Submit" value="提交" />
        <label>
         
        <input type="reset" name="Submit2" value="重置" />
      </label></td>
    </tr>
</table>
</form>
</body>
</html>

select.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>

<body>

<table width="862" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <form id="form1" name="form1" method="post" action="">
<tr>
      <td width="20" align="center" class="font1 td1"> </td>
      <td width="68" align="center" class="font1 td1">编号</td>
      <td width="169" align="center" class="font1 td1">投票数</td>
      <td width="409" align="center" class="font1 td1">
     <input name="opt" type="hidden" id="opt" />
        <input name="ID" type="hidden" id="ID" value="" />
        <input name="ObjID" type="hidden" id="ObjID" value="<?php echo $_REQUEST['ObjID']?>" />
        选项</td>
      <td width="160" align="center" class="font1 td1">操作</td>
    </tr>
<?php
$Sql="select * from tselect where ObjID='".$_REQUEST['ObjID']."' order by ID asc";
$Result=mysql_query($Sql);
while($Arr=@mysql_fetch_array($Result))
   {
?>
    <tr>
      <td bgcolor="#FFFFFF" class="font2"><input name="IDArr[]" type="checkbox" id="IDArr[]" value="<?php echo $Arr['ID']?>" /></td>
      <td align="center" bgcolor="#FFFFFF" class="font2"><?php echo $Arr['ID']?></td>
      <td align="center" bgcolor="#FFFFFF" class="font2"><?php echo $Arr['Num']?></td>
      <td align="left" bgcolor="#FFFFFF" class="font2"><?php echo $Arr['SName']?></td>
      <td align="center" bgcolor="#FFFFFF" class="font2">[<a href="javascript:objedit('selopt.php',document.form1,<?php echo $Arr['ID']?>)">编辑</a>][<a

href="javascript:objdel('selopt.php',document.form1,<?php echo $Arr['ID']?>)">删除</a>]</td>
    </tr>
    <?
   }
?>
<tr>
      <td colspan="3" align="center" bgcolor="#FFFFFF" class="font2">[<a href="javascript:selectAll(document.form1);">全选</a>][<a

href="javascript:selectOther(document.form1);">反选</a>][<a href="javascript:Objadd('selopt.php',document.form1);">增加</a>][<a href="javascript:Objdelsel

('selopt.php',document.form1);">删除</a>][<a href="index.php">返回</a>]</td>
      <td colspan="2" align="center" bgcolor="#FFFFFF" class="font2"> </td>
      </tr>
</form>
</table>

</body>
</html>

selopt.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
$opt=$_POST['opt'];
$ID=$_POST['ID'];
$IDArr=$_POST['IDArr'];
switch ($opt)
{
case "add":
    $OptName="添加新选项";
$OptStr="int";
break;
case "int":
    $Sql="insert into tselect (ObjID,SName,`Num`) values('".$_POST['ObjID']."','".$_POST['SName']."','0')";
if(@mysql_query($Sql))
   {
   echo "<script>alert('添加成功!');window.location.href='select.php?ObjID=".$_POST['ObjID']."';</script>";
   exit;
   }
else
   {
   echo "<script>alert('添加失败!');window.location.href='select.php?ObjID=".$_POST['ObjID']."';</script>";
   exit;
   }
break;
case "edit":
    $OptName="编辑选项";
$OptStr="upd";
$Sql="select * from tselect where ID='".$ID."'";
$Result=@mysql_query($Sql);
$Arr=@mysql_fetch_array($Result);
$SName=$Arr['SName'];
break;
case "upd":
    $Sql="update tselect set ObjID='".$_POST['ObjID']."',SName='".$_POST['SName']."' where ID='".$ID."'";
if(@mysql_query($Sql))
   {
   echo "<script>alert('编辑成功!');window.location.href='select.php?ObjID=".$_POST['ObjID']."';</script>";
   exit;
   }
else
   {
   echo "<script>alert('编辑失败!');window.location.href='select.php?ObjID=".$_POST['ObjID']."';</script>";
   exit;
   }
break;
case "del":
    $Sql="delete from tselect where ID='".$ID."'";
if(@mysql_query($Sql))
   {
   echo "<script>alert('删除成功!');window.location.href='select.php?ObjID=".$_POST['ObjID']."';</script>";
   exit;
   }
else
   {
   echo "<script>alert('删除失败!');window.location.href='select.php?ObjID=".$_POST['ObjID']."';</script>";
   exit;
   }
break;
case "delsel":
    $Count=count($IDArr);
if($Count>0)
   {
   $SqlStr="";
   for($i=0;$i<$Count;$i++)
     {
     $SqlStr.=" ID=".$IDArr[$i]." or ";
     }
   $SqlStr=substr($SqlStr,0,strlen($SqlStr)-4);
   $Sql="delete from tselect where ".$SqlStr;
   if(@mysql_query($Sql))
     {
     echo "<script>alert('删除成功!');window.location.href='select.php?ObjID=".$_POST['ObjID']."';</script>";
     exit;
     }
   else
     {
     echo "<script>alert('删除失败!');window.location.href='select.php?ObjID=".$_POST['ObjID']."';</script>";
     exit;
     }
   }
else
   {
   echo "<script>alert('提交参数错误,没有选择任何要删除的选项!');window.location.href='select.php?ObjID=".$_POST['ObjID']."';</script>";
   }
break;
}
?>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="862" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
      <td colspan="2" align="left" class="font1"> <?php echo $OptName?>
      <input name="ID" type="hidden" id="ID" value="<?php echo $_POST['ID'];?>" />
      <input name="ObjID" type="hidden" id="ObjID" value="<?php echo $_POST['ObjID']?>" />
      <input name="opt" type="hidden" id="opt" value="<?php echo $OptStr?>" /></td>
    </tr>
    <tr>
      <td width="242" align="right" bgcolor="#FFFFFF" class="font1">项目名称:</td>
      <td width="605" align="left" bgcolor="#FFFFFF" class="font2"><input name="SName" type="text" class="font3" id="SName" value="<?php echo $SName?>"

size="70" /></td>
    </tr>
</table>
<table width="862" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td height="38" align="center"><input type="submit" name="Submit" value="提交" />
        <label>
         
        <input type="reset" name="Submit2" value="重置" />
      </label></td>
    </tr>
</table>
</form>
</body>
</html>

数据库表结构:

--
-- 表的结构 `iptable`
--

CREATE TABLE `iptable` (
`ID` int(11) NOT NULL auto_increment,
`ObjID` int(11) NOT NULL,
`IP` varchar(50) default '0.0.0.0',
PRIMARY KEY (`ID`)
)

--
-- 表的结构 `tobject`
--

CREATE TABLE `tobject` (
`ID` int(11) NOT NULL auto_increment,
`OJBName` varchar(255) character set utf8 default NULL,
`SType` int(1) NOT NULL default '0',
PRIMARY KEY (`ID`)
)

--
-- 表的结构 `tselect`
--

CREATE TABLE `tselect` (
`ID` int(11) NOT NULL auto_increment,
`ObjID` int(11) default '0',
`SName` varchar(255) default NULL,
`Num` bigint(25) default '0',
PRIMARY KEY (`ID`)
)
 
 

抱歉!评论已关闭.