澳门新葡萄京官网首页php mysql 投票系统开发实例教程

如下:
?/******************************************************************************
Description: This is a simple script to send emails via a html-formto
different users Date: 1999-02-25 Author: amalesh kempf
amalesh@goatrance.deCreate this tableThe field what is for different
categoriesCREATE TABLE email_notify (ID int(11) DEFAULT ‘0’ NOT
NULL,What varchar(60) DEFAULT ‘0’ NOT NULL,Name varchar(60) DEFAULT ‘0’
NOT NULL,Email varchar(60) DEFAULT ‘0’ NOT NULL,timestamp
varchar(16),KEY (What),PRIMARY KEY (ID));To fill this table you might
create an insert
form*******************************************************************************///
Set this values:$strHost=localhost;$strUser=root;$strPassw
=;$strSender=you@domain.com;if (!$btnSendEmail){?The email will be added
automatically with Hello Name in the first line ofthe emailbody!brbrform
action=send_email.php3 enctype=application/x-
method=posttabletrtdSubject/tdtdinput name=strSubject
size=40/td/trtrtdBody/tdtdtextarea cols=40 name=strBody
rows=8wrap=PHYSICAL? echo $strBody
?/textarea/td/trtrtdCategory/tdtdselect name=strWhat?php // add you
categories here: ?option value=partyParty/option/select/td/tr/tableinput
name=btnSendEmail style=HEIGHT: 24px; WIDTH: 224pxtype=submit
value=Sende email/form?php}if (isset($btnSendEmail)){echo Send
Emailbr;// Create connection$intConID =
mysql_pconnect($strHost,$strUser,$strPassw);// Header$strHeader =
Return-Path: $strSender/nErrors-To: $strSender/nFrom:$strSender;//
SQL$strSQL = select name,email from email_notify where lcase(what)
=’$strWhat’;$intRes = mysql_query($strSQL,$intConID);echo Send Email
$strBodybr;// fetch arraywhile($saRow =
mysql_fetch_array($intRes)){$strEmail = $saRow[email];$strName =
$saRow[name];$strBodyComplete = Hello . $strName[$i] . !/n/n .
$strBody;//
Emailmail($strEmail,$strSubject,$strBodyComplete,$strHeader);//
Outputecho Send to $strNamebr;}}?

我们先来分析流程

一,数据库教程,

二,数据库文件dbconn.php

三,vote.php投票页面

四,投票功能页面vote_add.php

五,查看所有投票数据viewpoll.php

现在我们来看看数据库结构

 代码如下

drop database if exists vote;
create database vote;
use vote;

CREATE TABLE  title 
(                                                                                                                                                                                                                                                                
           id  int(6) NOT NULL auto_increment,
    username  varchar(20) NOT NULL,
    email  varchar(50) NOT NULL,
           title  varchar(250) NOT NULL default
”,                                                                                                                                                                                                                                           
           types  tinyint(2) NOT NULL default ‘1’,
    choice text,
           times  datetime NOT NULL default ‘0000-00-00
00:00:00’,                                                                                                                                                                                                                            
          primary KEY ( id )     
        );

CREATE TABLE  vote 
(                                                                                                                                                                                                                                                
           id  int(6) NOT NULL
auto_increment,                                                                                                                                                                                                                         
           titleid  int(6) NOT NULL default
‘0’,                                                                                                                                                                                                                           
           info  text NOT
NULL,                                                                                                                                                                                                                                              
           vcount  int(4) NOT NULL default
‘0’,                                                                                                                                                                                                                        
          primary KEY ( id
)                                                                                                                                                                                                                                             
        );
create table email (
    id int(6) not null,
    email varchar(50) not null,
    primary key ( id, email)
 );

数据库连接文件

 代码如下

<?php
$conn=mysql_connect(“localhost”,”phpdb”,”phpdb”)
        or die(“不能连接数据库服务器: “.mysql_error());
mysql_select_db(“vote”,$conn) or die (“不能选择数据库:
“.mysql_error());

?>

 

 

vote.php投票页面

 

 代码如下

<?php
// 包含数据库连接文件和头文件
require (‘dbconnect.php’);
include(‘head.php’);
// 获得参数,得到投票主题id
 $id=$_GET[id];
?>
<script
language=”javascript教程”>
    function checkvote()
    {    
  if (form1.email.value==””)
  {
   // 如果没有输入email,则显示警告信息
         alert(“请输入email!”);
   form1.email.focus();
   return false;
     }
   // 检查email格式是否正确
  else if (form1.email.value.charAt(0)==”.” ||
   form1.email.value.charAt(0)==”@”||
   form1.email.value.indexOf(‘@’,
0) == -1 ||
   form1.email.value.indexOf(‘.’, 0) == -1 ||
   form1.email.value.lastIndexOf(“@”)==form1.email.value.length-1 ||
   form1.email.value.lastIndexOf(“.”)==form1.email.value.length-1)
  {
   alert(“Email的格式不正确!”);
   form1.email.select();
   return false;
  }
  else{
   return true;
  }
    } 
</script>

<html>
<body>
<form name=”form1″ method=”post” action=”add_vote.php?id=<? echo
$id;?>” onsubmit=”return checkvote()”>
<table width=”70%” border=”1″ cellspacing=”1″ cellpadding=”3″
align=”center”>
    <tr>
      <th colspan=”2″>欢迎投票</th>
    </tr>
<? 
 // 构造读取数据的sql语句
 $titlesql=”select * from title where id=’$id'”;
 $titleresult=mysql_query($titlesql,$conn);
 $title=mysql_fetch_array($titleresult);
 echo “<tr>”;
 echo “<td width=’45%’>$title[title]”;
 if ($title[types]==1){
     echo “<br>(单选)</td>”;
 }
 else{
  echo “<br>(多选)</td>”;
 }
 // 从vote表中取出投票选项
 $votesql=”select * from vote where titleid=’$id'”;
 $voteresult=mysql_query($votesql,$conn);
 // 显示投票选项
 echo “<td><table>”;
 echo “<input type=’hidden’ name=’types’
value=’$title[types]’>”;
 while($info=mysql_fetch_array($voteresult)){
  echo “<tr><td width=’55%’>”;
  // 单选
  if ($title[types]==1){
   echo “<input type=’radio’ name=’select’ value=$info[id]>”;
   echo “$info[info]</td</tr>”;
  }
  // 多选
  else{
   echo “<input type=’checkbox’ name=’select[]’
value=$info[id]>”;
   echo “$info[info]</td</tr>”;
  }
 }
 echo “</table></td>”;
 echo “</tr>”;
?>
 <tr>
      <td >
        请输入您的Email:
      </td>
      <td>
        <input type=”text” name=”email” size=”50″
maxlength=”200″>
      </td>
    </tr>
 <tr>
      <td align=right >
        <input type=”submit” name=”Submit” value=”提交”>
      </td>
      <td>
        <input type=”reset” name=”Submit2″ value=”重置”>
      </td>
    </tr>
</table>
</form>
</body>
</html>

 

 

投票功能页面vote_add.php

 

<?php
// 包含数据库连接文件

 代码如下

require (‘dbconnect.php’);
?>
<?
// 取得网页的参数
$email=$_POST[’email’];
$select=$_POST[‘select’];
$types=$_POST[‘types’];
$id=$_GET[‘id’];

// 查看是否该email已经对该主题投过票,如果投过,则不允许再投
$emailsql=”select * from email where id=’$id’ and email=’$email'”;
$email_re=mysql_query($emailsql,$conn);
$email_arr=mysql_fetch_array($email_re);

if (!empty($email_arr)){
 echo “对不起,你已经投过票,不能重复投票!”;
 exit();
}
// 如果没有选择投票选项
if (!$select) {
 echo
“<script>alert(‘您还没有选择任何投票选项’);history.back();</script>”;
 exit();
}
// 没有投过,可以投票,记录该email
mysql_query(“insert into email(id,email)
values(‘$id’,’$email’)”,$conn)or die (“插入数据失败: “.mysql_error());

// 记录投票,分单选多选处理
if ($types==”1″) {
 $sql=”update vote set vcount=vcount+1 where id=$select”;
 $result=mysql_query($sql,$conn);
 echo
“<script>alert(‘投票成功!’);location.href=’viewpoll.php?id=$id’;</script>”;
}
else {
 for ($i=0;$i<sizeof($select);$i++) {
  $sql1=”update vote set vcount=vcount+1 where id=$select[$i]”;
  $result1=mysql_query($sql1,$conn);
 }
 echo
“<script>alert(‘投票成功!’);location.href=’viewpoll.php?id=$id’;</script>”;
}

// 关闭连接
mysql_close($conn);
?>

 

 

查看投票功能viewpoll.php

 

 代码如下

<?php
// 包含数据库连接文件和头文件
require (‘dbconnect.php’);
include(‘head.php’);
// 获得参数,得到投票主题id
 $id=$_GET[id];
?>
<?
// 取得投票主题标题
$titlesql=”select * from title where id=’$id'”;
$titleresult=mysql_query($titlesql,$conn);
$title=mysql_fetch_array($titleresult);

// 获得投票结果
$sql=”select * from vote where titleid= ‘$id'”;
$result=mysql_query($sql,$conn);

// 获得参与投票总人数
$emailsql=”select * from email where id=’$id'”;
$email_re=mysql_query($emailsql,$conn);
$votenum=mysql_num_rows($email_re);

?>
<table border=”0″ width=”80%” cellspacing=”0″ cellpadding=”0″
align=center>
<tr>
  <td width=”100%” colspan=”4″>
  <p align=”center”><font color=”#FF0000″>”<?echo
$title[title];?>”的投票结果</font></p>
  </td></tr>
  <tr>
  <td width=”100%” colspan=”4″>
  <p align=”center”>(共有<?echo
$votenum;?>人参与投票)</font></p>
  </td></tr>
<tr>
  <td width=”30%”> </td>
  <td width=”10%”> </td>
  <td width=”50%”> </td>
  <td width=”10%”> </td>
</tr>
<tr>
  <td width=”30%” align=”center”>
      <font size=”2″
color=”#0000FF”>选  项</font></td>
  <td width=”10%” align=”center”>
      <font size=”2″
color=”#0000FF”>票  数</font></td>
  <td width=”60%” colspan=”2″ align=”center”>
      <font size=”2″
color=”#0000FF”>比  例</font></td>
</tr>
<?
// 计算所有选项的总票数
$num=0;
while($row=mysql_fetch_array($result)){
 $num+=$row[vcount];
}
// 设置行指针
mysql_data_seek($result,0);
// 柱状显示各选项比例
while($bar=mysql_fetch_array($result)){
 echo “<tr>”;
 echo “<td width=’30%’ align=’center’>”;
    echo “<font size=’2′
color=’#0000FF’>$bar[info]</font></td>”;
 echo “<td width=’10%’ align=’center’>”;
    echo “<font size=’2′
color=’#0000FF’>$bar[vcount]</font></td>”;
 echo “<td width=’50%’ >”;
 // 计算比例
 $scale=(int)($bar[“vcount”]/$num*100);
 $bar_width=$scale*500/100;
 if ($scale!=0){
     echo “<img src=’./bar.gif’ width=$bar_width
height=10></td>”;
 }
 else{
  echo “</td>”;
 }
 echo “<td width=’10%’ align=’center’>”;
    echo “<font size=’2′
color=’#0000FF’>$scale%</font></td>”;
 echo “</tr>”;
}
?>

<tr>
  <td width=”30%”> </td>
  <td width=”10%”> </td>
  <td width=”50%”> </td>
  <td width=”10%”> </td>
</tr>

<!–<tr><td colspan=4 align=center><a
href=’circle_result.php?id=$id’>饼图显示结果</a></td></tr>–>
</table>

好了,这个简单的由php
mysql做的投票系统实例教程也就讲完了,喜欢就告诉你的朋友一起来学习吧。

发表评论

电子邮件地址不会被公开。 必填项已用*标注