投票系统例题

复选框选择,点击提交上传数据库,点击查看显示百分比

封装类的代码

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/16
 * Time: 11:23
 */
class DBDA
{
    public $host="localhost";//服务器地址
    public $uid="root" ;//用户名
    public $pwd="";//密码

    public $dbconnect;//链接对象

    function Query($sql,$type=1,$dbname="aaas")//sql语句;type语句类型,数字来表示;数据库的默认值
    {
        $this->dbconnect = new mysqli($this->host,$this->uid,$this->pwd,$dbname);
        //判断是否出错
        if(!mysqli_connect_error())
        {
            //如果连接成功,执行SQL语句
            $result = $this->dbconnect->query($sql);

            //根据语句类型判断
            if($type==1)
            {
                //如果是查询语句,返回二维数组
                return $result->fetch_all();
            }
            else
            {
                //如果是其他语句,返回true或false
                return $result;
            }
        }
        else
        {
            return "连接失败!";
        }
    }

}
<!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=utf-8" />
    <title>投票系统</title>

    <style type="text/css">
        .x
        {
            float:left;
        }
        #top
        {
             500px;
            height: 200px;
        }

        #bottom
        {
             500px;
            height: 200px;
            display: none;
        }
        #fanhui
        {
            display: none;
        }
        </style>
</head>

<body>
<form action="0616toupiaocl.php" method="post">


<?php
include("0616DBDA.class.php");
$db=new DBDA();//造对象
$stm="select * from diaoyantimu limit 0,1";//查出数据,取第一条
$atm=$db->query($stm);//执行,返回二维数组

echo "<div>题目名称:{$atm[0][1]}</div>";//显示题目名称
//输出选项
$atm[0][0];//题目代号
//查调研选项表
$sxx="select * from diaoyanxuanxiang where timudaihao='{$atm[0][0]}'";
$axx=$db->query($sxx);//存放选项的二维数组

echo "<div id='top'>";

foreach($axx as $v)
{
    echo
    "<div>
        <input type='checkbox' value='{$v[0]}' name='xx[]'>{$v[1]}
    </div>";
}

echo "</div>";

?>

 <div id="bottom">

    <?PHP

    $sum="select sum(numbers) from diaoyanxuanxiang where timudaihao='{$atm[0][0]}'";//查出数目总和
    $asum=$db->query($sum);
    $total=$asum[0][0];//总人数

    foreach($axx as $v)
    {

        $bfb=($v[2]/$total)*100;//投该选项的人数百分比


        echo "<div><span class='x'>{$v[1]}</span>

        <div class='x' style='border:2px solid #a414a1;120px; height:12px'>
        <div style='background-color:#292aff;{$bfb}%;height:12px'></div>
        </div>

        <span class='x'>{$v[2]}</span>
        <span class='x'>   {$bfb}%</span>
</div><br>";
    }


    ?>

        </div>

<div id="anniu"><input type="submit" value="提交"><input type="button" value="查看" onclick="showresult()"></div>
<div id="fanhui"><input type="button" value="返回" onclick="show()"></div>
</form>

</body>

<script type="text/javascript">
    function showresult()
    {
        document.getElementById("top").style.display="none";
        document.getElementById("bottom").style.display="block";
        document.getElementById("fanhui").style.display="block";
        document.getElementById("anniu").style.display="none";
    }

    function show()
    {
        document.getElementById("top").style.display="block";
        document.getElementById("bottom").style.display="none";
        document.getElementById("fanhui").style.display="none";
        document.getElementById("anniu").style.display="block";
    }

</script>
</html>

  

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/17
 * Time: 10:53
 */

$ids=$_POST["xx"];

include("0616DBDA.class.php ");
$db=new DBDA();

foreach($ids as $v)
{
    $sql= "update diaoyanxuanxiang set numbers=numbers+1 where ids='{$v}'";
    $db->query($sql,0);
}

header("location:0617toupiao.php");

  

 

原文地址:https://www.cnblogs.com/pangchunlei/p/5596018.html