Home > Backend Development > PHP Tutorial > Questionnaire implemented by php+mysql+xml (code)

Questionnaire implemented by php+mysql+xml (code)

不言
Release: 2023-04-04 09:48:02
forward
4158 people have browsed it

The content of this article is a questionnaire survey (code) about the implementation of php mysql xml. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I completed a questionnaire based on the book PHP Typical Modules and Project Practice. At the same time, the administrator can modify the survey content

At the same time, a css file was used, which was borrowed from

http://www.wufangbo.com/p-css-vote/

Requires 5 php files: admin.php, update.php, view.php, result.php, vote.php ,

First create a new folder under WWW, here it is named "diaocha". My port number is 8080

The database is as follows: the name of the library is cms_vote and the name of the table is:vote


The content inside the Votenote is:

<movies>
<movie>
<title>您想去哪个城市:</title>
<id>1</id>
<count>350</count>
<content>广东</content>
</movie>
<movie>
<title>您想去哪个城市:</title>
<id>2</id>
<count>47</count>
<content>湖南</content>
</movie>
<movie>
<title>您想去哪个城市:</title>
<id>3</id>
<count>118</count>
<content>上海</content>
</movie>
<movie>
<title>您想去哪个城市:</title>
<id>4</id>
<count>122</count>
<content>北京</content>
</movie>
<movie>
<title>您想去哪个城市:</title>
<id>5</id>
<count>80</count>
<content>湖北</content>
</movie>
</movies>
Copy after login

1 , admin.php

<?php
$con = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;)or die("could not connect database");//此处根据自己的数据库的名字,密码进行修改
mysql_query("set names utf8");
mysql_select_db(&#39;cms_vote&#39;)or die("could not select database");
$aid = $_GET[&#39;aid&#39;];
if(!empty($aid)){
$sql=&#39;SELECT *FROM vote &#39;;
//WHERE aid=".$aid."&#39;;
$result = mysql_query($sql,$con);
$row = mysql_fetch_array($result);
}
else{
echo &#39;<script>alert(\&#39;调查异常\&#39;);</script>&#39;;
exit;
}
?>
Copy after login


调查管理
调查项目:
调查总人数:
开始时间:
结束时间:
投票项:
Copy after login


2, update.php

<?php
$con = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;)or die("could not connect database");//此处根据自己的数据库的名字,密码进行修改
mysql_query("set names utf8");
mysql_select_db(&#39;cms_vote&#39;)or die("could not select database");
$aid = $_POST[&#39;aid&#39;];
$votename=$_POST[&#39;votename&#39;];
$starttime=$_POST[&#39;starttime&#39;];
$endtime=$_POST[&#39;endtime&#39;];
$votenote=$_POST[&#39;votenote&#39;];
$totalcount=$_POST[&#39;totalcount&#39;];
if(!empty($aid))
{
$sql = "UPDATE  vote SET Votename= &#39;".$votename."&#39;, 
Starttime=&#39;".$starttime."&#39;, Endtime=&#39;".$endtime."&#39;, 
Totalcount=&#39;".$totalcount."&#39;, Votenote=&#39;".$votenote."&#39; WHERE 
aid=&#39;".$aid."&#39;order by aid desc limit 1;";
$result = mysql_query($sql);
if(!empty($result)){
echo &#39;<script>alert(\&#39;数据保存成功\&#39;);</script>&#39;;
echo "您的调查问卷已生效!";
}
else
{
echo &#39;<script>alert(\&#39;数据保存失败\&#39;);</script>&#39;;
exit();
}
}
else{
echo &#39;<script>alert(\&#39;调查异常\&#39;);</script>&#39;;
exit();
}
?>
Copy after login

Verification is successful: http://localhost:8080/diaocha/admin.php? aid=1

(Note that you need to add ?aod=1) The format is in English format

When you press the save survey data button: # will appear

##It will appear that your questionnaire has taken effect

3, view.php

<?php
$voteitem = $_POST[&#39;voteitem&#39;];
$con = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;)or die("could not connect database");//密码用户名按照自己的修改
mysql_query("set names utf8");
mysql_select_db(&#39;cms_vote&#39;)or die("could not select database");
$sql="SELECT *FROM vote";
$result = mysql_query($sql,$con);
$arr = mysql_fetch_array($result);
$xmlstr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$xmlstr.=$arr[&#39;Votenote&#39;];
$xml = simplexml_load_string($xmlstr);
$b[]=Array();
$a[]=Array();
for($i=0;$i<5;$i++){
$b[$i]=$xml->movie[$i]->count;
$a[$i]=$xml->movie[$i]->count+1;
}
for($i=0;$i<5;$i++){
$b[$i]=$a[$i];
$xmlstra[$i]="<movies>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>1</id>
<count>".$b[0]."</count>
<content>". $xml->movie[0]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>2</id>
<count>".$b[1]."</count>
<content>". $xml->movie[1]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>3</id>
<count>".$b[2]."</count>
<content>". $xml->movie[2]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>4</id>
<count>".$b[3]."</count>
<content>". $xml->movie[3]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>4</id>
<count>".$b[4]."</count>
<content>". $xml->movie[4]->content ."</content>
</movie>
</movies>";
}
if($voteitem!=null){
$sql = "UPDATE  vote SET Totalcount=Totalcount+1,Votenote=&#39;".$xmlstra[$voteitem]."&#39; order by aid desc limit 1";
$result = mysql_query($sql);
$url = &#39;result.php?aid=1&#39;;
switch($voteitem){
case 0:
{
if(!empty($result)){
echo &#39;<script>alert("投票1成功!");location.href="&#39;.$url.&#39;"</script>&#39;;
exit();
}
break;
}
case 1:
{
if(!empty($result)){
echo &#39;<script>alert("投票2成功!");location.href="&#39;.$url.&#39;"</script>&#39;;
exit();
}
break;
}
case 2:
{
if(!empty($result)){
echo &#39;<script>alert("投票3成功!");location.href="&#39;.$url.&#39;"</script>&#39;;
exit();
}
break;
}
case 3:
{
if(!empty($result)){
echo &#39;<script>alert("投票4成功!");location.href="&#39;.$url.&#39;"</script>&#39;;
exit();
}
break;
}
case 4:
{
if(!empty($result)){
echo &#39;<script>alert("投票5成功!");location.href="&#39;.$url.&#39;"</script>&#39;;
exit();
}
break;
}
}
}
else{
echo &#39;<script>window.close();</script>&#39;; 
}
?>
Copy after login

The following results will appear

4.vote.php

<?php
$voteitem = $_POST[&#39;voteitem&#39;];
$con = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;)or die("could not connect database");
mysql_query("set names utf8");
mysql_select_db(&#39;cms_vote&#39;)or die("could not select database");
$sql="SELECT *FROM vote";
$result = mysql_query($sql,$con);
$arr = mysql_fetch_array($result);
$xmlstr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$xmlstr.=$arr[&#39;Votenote&#39;];
$xml = simplexml_load_string($xmlstr);
$b[]=Array();
$a[]=Array();
for($i=0;$i<5;$i++){
$b[$i]=$xml->movie[$i]->count;
$a[$i]=$xml->movie[$i]->count+1;
}
for($i=0;$i<5;$i++){
$b[$i]=$a[$i];
$xmlstra[$i]="<movies>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>1</id>
<count>".$b[0]."</count>
<content>". $xml->movie[0]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>2</id>
<count>".$b[1]."</count>
<content>". $xml->movie[1]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>3</id>
<count>".$b[2]."</count>
<content>". $xml->movie[2]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>4</id>
<count>".$b[3]."</count>
<content>". $xml->movie[3]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>4</id>
<count>".$b[4]."</count>
<content>". $xml->movie[4]->content ."</content>
</movie>
</movies>";
}
if($voteitem!=null){
$sql = "UPDATE  vote SET Totalcount=Totalcount+1,Votenote=&#39;".$xmlstra[$voteitem]."&#39; order by aid desc limit 1";
$result = mysql_query($sql);
$url = &#39;result.php?aid=1&#39;;
switch($voteitem){
case 0:
{
if(!empty($result)){
echo &#39;<script>alert("投票1成功!");location.href="&#39;.$url.&#39;"</script>&#39;;
exit();
}
break;
}
case 1:
{
if(!empty($result)){
echo &#39;<script>alert("投票2成功!");location.href="&#39;.$url.&#39;"</script>&#39;;
exit();
}
break;
}
case 2:
{
if(!empty($result)){
echo &#39;<script>alert("投票3成功!");location.href="&#39;.$url.&#39;"</script>&#39;;
exit();
}
break;
}
case 3:
{
if(!empty($result)){
echo &#39;<script>alert("投票4成功!");location.href="&#39;.$url.&#39;"</script>&#39;;
exit();
}
break;
}
case 4:
{
if(!empty($result)){
echo &#39;<script>alert("投票5成功!");location.href="&#39;.$url.&#39;"</script>&#39;;
exit();
}
break;
}
}
}
else{
echo &#39;<script>window.close();</script>&#39;; 
}
?>
Copy after login

5.result.php

<?php
$con = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;)or die("could not connect database");
mysql_query("set names utf8");
mysql_select_db(&#39;cms_vote&#39;)or die("could not select database");
$aid = $_GET[&#39;aid&#39;];
if(!empty($aid)){
$sql="SELECT *FROM vote WHERE aid=&#39;".$aid."&#39;";
$result = mysql_query($sql,$con);
$arr = mysql_fetch_array($result);
$xmlstr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$xmlstr.=$arr[&#39;Votenote&#39;];
$xml = simplexml_load_string($xmlstr);
$n0=$xml->movie[0]->count/$arr[&#39;Totalcount&#39;];
$n1=$xml->movie[1]->count/$arr[&#39;Totalcount&#39;];
$n2=$xml->movie[2]->count/$arr[&#39;Totalcount&#39;];
$n3=$xml->movie[3]->count/$arr[&#39;Totalcount&#39;];
$n4=$xml->movie[4]->count/$arr[&#39;Totalcount&#39;];
}
/*else
{
echo &#39;<script>alert(\&#39;调查异常\&#39;);</script>&#39;;
exit;
}*/
?>
Copy after login
<html>
<head>
<style >
#graphbox{
border:1px solid #e7e7e7;
padding:10px;
width:545px;
background-color:#f8f8f8;
margin:5px 0;//这是最大的一个div
}
.itemname{
width:70px;
font-weight:700;
font-size:14px;
line-height:18px;
height:18px;
padding:2px;
text-align:right;
margin-right:atuo;
}
.percent{
width:150px;
float:right;
font-size:13px;
line-height:18px;
height:18px;
padding:2px;
color:#555;
text-align:left;
margin-right:3px;
}
.graph{
position:relative;
background-color:#F0EFEF;
border:1px solid #cccccc;
font-size:13px;
width:300px;
font-weight:700;
float:right;
margin-right:3px;
}
.color1, .color2, .color3, .color4, .color5{
position:relative;
text-align:left;
color:#ffffff;
height:18px;
display:block;
}
.graph .color1{background-color:#afb4db;}
.graph .color2{background-color:#84bf96;}
.graph .color3{background-color:#ea66a6;}
.graph .color4{background-color:#50b7c1;}
.graph .color5{background-color:#ffd400;}
.font1{color:#669999;}
.font2{color:#6699FF;}
.font3{color:#FF9900;}
.font4{color:#FF3333;}
</style>
<body>
<h3><?php echo $arr[&#39;Votename&#39;]; ?></h3>
<td>调查结果</td>
<div>
<td>调查开始时间:<?php
 echo $arr[&#39;Starttime&#39;]; ?>    调查结束时间:<?php echo $arr[&#39;Endtime&#39;]; 
?>    调查总人数:<?php echo $arr[&#39;Totalcount&#39;]; 
?></td></td>
<div>
<div id="graphbox">
 
<div class="little_box">
<div
 class="percent"><?php echo sprintf("%01.0f",$n0*100).&#39;%&#39;;?> 
<?php echo $xml->movie[0]->count;?></div>
<div class="graph">
<span class="color1" style="width:<?php echo sprintf("%01.0f",$n0*100).&#39;%&#39;;?>"> </span></div>
<div class="itemname font1"><?php  echo $xml->movie[0]->content;?></div>
</div>
 
<div class="little_box">
<div
 class="percent"><?php echo sprintf("%01.0f",$n1*100).&#39;%&#39;;?> 
<?php echo $xml->movie[1]->count;?></div>
<div class="graph">
<span class="color2" style="width:<?php echo sprintf("%01.0f",$n1*100).&#39;%&#39;;?>"> </span></div>
<div class="itemname font2"><?php  echo $xml->movie[1]->content;?></div>
</div>
 
<div class="little_box">
<div
 class="percent"><?php echo sprintf("%01.0f",$n2*100).&#39;%&#39;;?> 
<?php echo $xml->movie[2]->count;?>  </div>
<div class="graph">
<span class="color3" style="width:<?php echo sprintf("%01.0f",$n2*100).&#39;%&#39;;?>;"> </span></div>
<div class="itemname font3"><?php  echo $xml->movie[2]->content;?></div>
</div>
 
<div class="little_box">
<div
 class="percent"><?php echo sprintf("%01.0f",$n3*100).&#39;%&#39;;?> 
<?php echo $xml->movie[3]->count;?>  </div>
<div class="graph">
<span class="color4" style="width:<?php echo sprintf("%01.0f",$n3*100).&#39;%&#39;;?>;"> </span></div>
<div class="itemname font4"><?php  echo $xml->movie[3]->content;?></div>
</div>
 
<div class="little_box">
<div
 class="percent"><?php echo sprintf("%01.0f",$n4*100).&#39;%&#39;;?> 
<?php echo $xml->movie[4]->count;?>  </div>
<div class="graph">
<span class="color5" style="width:<?php echo sprintf("%01.0f",$n4*100).&#39;%&#39;;?>;"> </span></div>
<div class="itemname font5"><?php  echo $xml->movie[4]->content;?></div>
</div>
 
</div>
</body>
</head>
</html>
Copy after login

The above is the detailed content of Questionnaire implemented by php+mysql+xml (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template