Home > Article > Backend Development > php voting (survey)_PHP tutorial
/**
* Poll
*
*
**/
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='poll.php?action=polllist'</script>";
}else{
echo "<script>alert('失败!');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='poll.php?action=polllist'</script>";
}else{
echo "<script>alert('失败!');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.="