>  기사  >  백엔드 개발  >  php 投票(调查)_PHP教程

php 投票(调查)_PHP教程

WBOY
WBOY원래의
2016-07-13 17:49:08805검색

/**
 * 投票调查
 * 
 * 
 * */ 
include "isLogin.php"; 
include "../conn/config.inc.php"; 
/*action操作初始化*/ 
if(emptyempty($_REQUEST['action'])){ 
    $_REQUEST['action']='list'; 
}else { 
    $_REQUEST['action']=trim($_REQUEST['action']); 

 
if($_REQUEST['action']=='add'){ 
    $tpl->assign('submitButton','添加'); 
    $tpl->assign('tmess','添加投票选项'); 
    $tpl->assign('act','insert'); 
    $tpl->display('admin/addpoll.tpl'); 
}elseif ($_REQUEST['action']=='insert'){ 
    $title=$_POST['title']; 
    $desc=$_POST['desc']; 
    $addtime=time(); 
    $poll=$_POST['poll']; 
 
    $sql="insert into  poll (title,des,addtime) values ('$title','$desc','$addtime')"; 
    $rs=$db->query($sql); 
    $lastid=$db->insert_id(); 
 
   foreach ($poll as $v){ 
   $sql2="insert into poll_option (pollid,opdata) values ('$lastid','$v')"; 
   $rs2=$db->query($sql2); 
     } 
    if($rs&&$rs2){ 
        echo "<script>location.href=&#39;poll.php?action=polllist&#39;</script>"; 
    }else{ 
        echo "<script>alert(&#39;失败!&#39;);history.go(-1)</script>"; 
    } 

//投票列表 
elseif ($_REQUEST['action']=='polllist'){ 
//  $sql="select * from poll order by id desc"; 
//  $rs=$db->query($sql); 
//  $poll=$db->fetch_array($rs); 
    $poll=getpoll(); 
    $tpl->assign('poll',$poll); 
    $tpl->assign('tmess','投票列表'); 
    $tpl->display('admin/polllist.tpl'); 

//删除 
elseif ($_REQUEST['action']=='del'){ 
    $id=$_GET['id']; 
     $sql="delete from poll where id={$id}"; 
     $sql2="delete from poll_option where pollid={$id}"; 
//echo  $sql="delete from poll INNER JOIN poll_option ON poll_option.pollid=poll.id where id={$id}"; 
    $rs=$db->query($sql); 
    $rs2=$db->query($sql2); 
    if($rs&&$rs2){ 
        echo "<script>location.href=&#39;poll.php?action=polllist&#39;</script>"; 
    }else{ 
        echo "<script>alert(&#39;失败!&#39;);history.go(-1)</script>"; 
    } 

elseif ($_REQUEST['action']=='edit'){ 
    $id=intval($_GET['id']); 
//  $sql="SELECT p.id, p.title, p.des, p.addtime, po.id, po.pollid, po.opdata 
//         FROM poll AS p 
//         LEFT JOIN poll_option AS po ON p.id = po.pollid 
//         WHERE p.id ={$id}"; 
    $sql="select * from poll where id={$id}"; 
    $rs=$db->query($sql); 
    $row=$db->fetch_array($rs); 
    $sql2="select* from poll_option where pollid={$id} order by id ASC"; 
    $rs2=$db->query($sql2); 
    $i=0; 
    $potion=''; 
    while ($row2=$db->fetch_array($rs2)){ 
        $i++; 
        $potion.="

"; 
        if($i>2){ 
        $potion.=""; 
        } 
        $potion.="
"; 
    } 
    $tpl->assign('pp',$potion); 
    $tpl->assign('post',$row); 
    $tpl->assign('tmess','更新投票'); 
    $tpl->assign('submitButton','更新'); 
    $tpl->assign('act','update'); 
    $tpl->display('admin/editpoll.tpl'); 

//更新投票信息  www.2cto.com
elseif ($_REQUEST['action']=='update'){ 
     
    $id=$_POST['id']; 
    $title=$_POST['title']; 
    $desc=$_POST['desc']; 
    $addtime=time(); 
    $poll=$_POST['poll']; 
    $sql="update poll SET title='{$title}',des='{$desc}',addtime='{$addtime}' where id='{$id}'"; 
    $rs=$db->query($sql); 
    foreach ($poll as $k=> $v){ 
//      echo "ID:是".$k."值是:".$v; 
//      echo "
"; 
        $sql2="update poll_option SET opdata='{$v}' where id='{$k}' and pollid={$id} "; 
        $rs2=$db->query($sql2); 
    } 
    if($rs&&$rs2){ 
        echo "<script>location.href=&#39;poll.php?action=polllist&#39;</script>"; 
    }else{ 
        echo "<script>alert(&#39;更新失败!&#39;);history.go(-1)</script>"; 
    } 

 
 
 
 
//==============投票相关函数================= 
//获取投票信息 
function  getpoll(){ 
    global $db; 
    $sql="select * from poll order by id desc"; 
    $rs=$db->query($sql); 
    $data=array(); 
    while ($row=$db->fetch_array($rs)){ 
        $data[]=$row; 
    } 
    return $data; 
}
 
?> 

摘自 chaojie2009的专栏

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478383.htmlTechArticle?php /** * 投票调查 * * * */ include isLogin.php; include ../conn/config.inc.php; /*action操作初始化*/ if(emptyempty($_REQUEST[action])){ $_REQUEST[action]=list; }else { $_...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.