Home > Backend Development > PHP Tutorial > Simple online voting system php - publish voting page

Simple online voting system php - publish voting page

WBOY
Release: 2016-07-29 09:07:02
Original
2301 people have browsed it

Publishing the page can only be done when the user is logged in, so you need to check whether you are logged in first, and then allow voting to be posted after confirmation

<center><input type="submit" name="send" value="发布"><li>注意:一旦发布将不能修改!</li></center>
Copy after login

The information entered by the user will be published to the background in post format.

<?php
//发布
if(@$_POST[&#39;send&#39;]){
    $ouser=@$_GET[&#39;user&#39;];
    if($ouser != &#39;&#39;){
        $vname=@$_POST[voteName];
        $time=time();
        $starttime=date("y-m-d",$time);
        $vintro=@$_POST[voteIntro];
        $open=@$_POST[voteOpen];
        $more=@$_POST[voteSel];
        $max=@$_POST[itemMax];
        $min=@$_POST[itemMin];
        $itemcount=@$_POST[voteCount];
        //为了方便最后完成字符串,让$item先等于$_POST[1],这样在下面的for循环中要少循环一次
        $item=@$_POST[1];
        $num=0;
        if($max>=$min && $vname!=""){
            for($i=1;$i<$itemcount;$i++){
                $j=$i+1;
                $item=$item.&#39;*&#39;.@$_POST[$j];
                $num=$num.&#39;*0&#39;;
            }
            $sql3="SELECT uid FROM users WHERE username=&#39;$ouser&#39;";
            $getVid=mysql_query($sql3);
            $row=mysql_fetch_row($getVid);
            $uid=$row[0];
            $sql="insert into vote(vname,starttime,uid,vintro,open,more,max,min) value(&#39;$vname&#39;,&#39;$starttime&#39;,&#39;$uid&#39;,&#39;$vintro&#39;,&#39;$open&#39;,&#39;$more&#39;,&#39;$max&#39;,&#39;$min&#39;)";
            mysql_query($sql);
            $sql2="SELECT vid FROM vote WHERE vname=&#39;$vname&#39;";
            $getVid=mysql_query($sql2);
            $row=mysql_fetch_row($getVid);
            $vid=$row[0];
            $sql4="insert into votetitle(vid,itemcount,item,num) value(&#39;$vid&#39;,&#39;$itemcount&#39;,&#39;$item&#39;,&#39;$num&#39;)";
            mysql_query($sql4);
            echo "<script language=&#39;javascript&#39;> alert('成功提交!'); </script>";
        }
        else{
            echo "<script language=&#39;javascript&#39;> alert('选项设置有误!'); </script>";
        }
    }else{
        echo "<script language=&#39;javascript&#39;> alert('请先登录!'); </script>";
    }
}
?>
Copy after login

1. Check whether you are logged in

    $ouser=@$_GET['user'];
    if($ouser != ''){
Copy after login
2. Get the current time
        $time=time();
        $starttime=date("y-m-d",$time);
Copy after login
3. Form option information and voting statistical information that can be saved in the database

   $item=@$_POST[1];
        $num=0;
        if($max>=$min && $vname!=""){
            for($i=1;$i<$itemcount;$i++){
                $j=$i+1;
                $item=$item.'*'.@$_POST[$j];
                $num=$num.'*0';
            }
Copy after login
(1) Option information and voting statistical information are both obtained by combining the option information Use "*" to separate them to form a string

Example: "Option 1*Option 2*Option 3"

(2) Default initial voting statistics, all items are 0.

Example: "0*0*0"

(3) This loop statement is actually unnecessary. PHP provides special methods to integrate and decompose arrays into strings (explode and implode), but in When I completed this part of the code, I didn't know the application of this method, so I completed this function myself.

The above introduces the simple online voting system PHP - publishing the voting page, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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