PHP投票练习
1、连接数据库查询数据显示在页面中
2、将需要改变数据的主键值提交到处理页面
3、处理页面中通过主键值找到数据中的计数列的值,将计数列的的值加1复制给计数列
4、在另一个页面显式结果
可以复制粘贴运行:
数据库表
效果图
结果图:
主页面
<!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>
<?php
include ("DBDA.class.php");
$db = new DBDA();
$sql = "select * from DiaoYanTiMu";
$tm = $db->Query($sql);
$sql = "select * from DiaoYanXuanXiang where TiMuDaiHao = '{$tm[0][0]}'";
$xx = $db->Query($sql);
echo "<div style=\"width:100%; text-align:center;\">";
echo "<h1>{$tm[0][1]}</h1>";
?>
<form action="tianjia.php" method="post">
<table width="150px" style="margin:0 auto; text-align:left;" >
<?php
foreach($xx as $v){
echo "<tr>
<td><input type=\"checkbox\" name=\"xx[]\" value=\" $v[0] \" /> $v[1] </td>
</tr>";
}
?>
</table>
<div><input type="submit" value="提交" />
<a href="jieguo.php"><input type="button" value="查看结果" /></a></div>
</form>
</div>
<div style="width:100%; text-align:center;">
<h1><?php echo $tm[1][1]; ?></h1>
<form action="tianjia1.php" method="post">
<table width="300px" style="margin:0 auto; text-align:left;" >
<?php
$sql = "select * from DiaoYanXuanXiang where TiMuDaiHao = '{$tm[1][0]}'";
$xx = $db->Query($sql);
foreach($xx as $v){
echo "<tr>
<td><input type=\"radio\" name=\"xx\" value=\" $v[0] \" /> $v[1] </td>
</tr>";
}
?>
</table>
<div><input type="submit" value="提交" />
<a href="jieguo.php"><input type="button" value="查看结果" /></a></div>
</form>
</div>
</body>
</html>
处理页面:
<?php
include ("DBDA.class.php");
$xx = $_POST["xx"];
$db = new DBDA();
var_dump($xx);
foreach($xx as $v){
$sql = "select numbers from diaoyanxuanxiang where ids = {$v}";
$num = $db->Query($sql);
var_dump($num);
$sql = "update diaoyanxuanxiang set numbers = ({$num[0][0]}+1) where ids = {$v}";
if(!$db->Query($sql,0)){
echo "添加错误";
}
}
header("location:toupiao.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=utf-8" />
<title>无标题文档</title>
</head>
<body>
<?php
include ("DBDA.class.php");
$db = new DBDA();
$sql = "select * from DiaoYanTiMu";
$tm = $db->Query($sql);
$sql = "select * from DiaoYanXuanXiang where TiMuDaiHao = '{$tm[0][0]}'";
$xx = $db->Query($sql);
$sql = "select sum(numbers) from diaoyanxuanxiang where timudaihao = '{$tm[0][0]}'";
$sum = $db->Query($sql);
echo "<div style=\"width:100%; text-align:center;\">";
echo "<h1>{$tm[0][1]}</h1>";
?>
<table width="400px" style="margin:0 auto; text-align:left;" cellpadding="0" cellspacing="0" >
<?php
foreach($xx as $v){
$ba = floor(($v[2]/$sum[0][0])*10000);
$bb = $ba/100;
echo "<tr>
<td>{$v[1]}</td>
<td>
<div style=\"height:10px; width:150px; border:1px solid #000;\">
<div style=\"height:10px; width: $bb% ; background-color:#F00\"></div>
</div>
</td>
<td>{$v[2]}</td>
<td>($bb%)</td>
</tr>";
}
?>
</table>
<?php
echo "<h1>{$tm[1][1]}</h1>";
$sql = "select * from DiaoYanXuanXiang where TiMuDaiHao = '{$tm[1][0]}'";
$xx = $db->Query($sql);
$sql = "select sum(numbers) from diaoyanxuanxiang where timudaihao = '{$tm[1][0]}'";
$sum = $db->Query($sql);
?>
<table width="400px" style="margin:0 auto; text-align:left;" cellpadding="0" cellspacing="0" >
<?php
foreach($xx as $v){
$ba = floor(($v[2]/$sum[0][0])*10000);
$bb = $ba/100;
echo "<tr>
<td>{$v[1]}</td>
<td>
<div style=\"height:10px; width:150px; border:1px solid #000;\">
<div style=\"height:10px; width: $bb% ; background-color:#F00\"></div>
</div>
</td>
<td>{$v[2]}</td>
<td>($bb%)</td>
</tr>";
}
?>
</table>
</div>
<div style="width:100%; text-align:center;">
<a href="toupiao.php"><input type="button" value="返回" /></a></div>
</body>
</html>