案例:投票

如果你没学好,只能说明你不够努力,仅此。

封装类DBDA页面:

<?php
header("content-type:text/html;charset=utf-8");
class DBDA
{
    public $host="localhost";
    public $uid="root";
    public $pwd="123";
    public $dbconnect;
    function query($sql,$type=1,$dbname="diaocha")
    {
        $this->dbconnect=new MySQLi($this->host,$this->uid,$this->pwd,$dbname);
        if(!mysqli_connect_error())
        {
            $result=$this->dbconnect->query($sql);
            if($type==1)
            {
                return $result->fetch_all();
            }
            else
            {
                return $result;
            }
        }
        else 
        {
            echo "连接失败";
        }
    }
}

主页面:

<!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>
</head>

<body>
<div>
<?php
include "DBDA.CLASS.PHP";
$timu=new DBDA();
$sql="select title from diaoyantimu where ids=1";
$a=$timu->query($sql);
foreach($a as $v)
{
    echo "{$v[0]}";
}
?>
<form action="result1.php" method="post">
<?php
$dx=new DBDA();
$sql="select * from diaoyanxuanxiang where timudaihao=1";
$b=$dx->query($sql);
foreach($b as $n)
{
    echo "<input type='checkbox' name='check[]' value='{$n[0]}'/>{$n[1]}<br />";
}
?>
<input type="submit" value="提交"/>
<a href="result.php"><input type="button" value="查看结果"/></a>
</form>
</div>
</body>
</html>

点击查看结果按钮(butturn)效果页面:

<body>
<?php
include "DBDA.class.php";
$xx=new DBDA();
//输出题目
$s="select * from diaoyantimu where ids=1";
$shuzu=$xx->query($s);
echo $shuzu[0][1];
//输出选项
$sql="select * from diaoyanxuanxiang where timudaihao=1";
$attr=$xx->query($sql);
$sq="select sum(numbers) from diaoyanxuanxiang where timudaihao=1";
$at=$xx->query($sq);
$total=$at[0][0];
foreach($attr as $v)
{    
    $d=($v[2]/$total)*100;
    
    echo "<div>
    <span style='float:left;'>{$v[1]}&nbsp;</span>
    <div style='100px; height:15px; border:1px solid #000000; float:left'>
    <div style='{$d}%;height:15px; background-color:red;'>
    </div>
    </div>;
    <span style='float:left'>&nbsp;{$v[2]}</span>
    <span style='float:left'>&nbsp;{$d}%</span>
    </div>";        
}
?>
<a href="prctice.php"><input style="cursor:pointer;" type="button" value="返回"/></a>
</body>

提交处理页面:

<?php
//提交
include "DBDA.class.php";
$dx=new DBDA();
if(!empty($_POST))
{
$v1=$_POST["check"];//获取到的是数组
var_dump($v1);
foreach($v1 as $v2)
{
    $s="update diaoyanxuanxiang set numbers=numbers+1 where ids={$v2}";
    var_dump($s);
    $dx->query($s,2);
    //var_dump($dx->query($s,0));
}
}
header("location:result.php");
原文地址:https://www.cnblogs.com/jinshui/p/5594249.html