投票系统投票界面

投票界面

投票界面包括用户的登陆,所有投票题目的呈现,每个投票信息的具体呈现
用户登录界面的具体代码如下

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0,width=device-width" /> 
<title>用户登录</title>
<script type="text/javascript" src="js/jquery.min.js"></script>

<link rel="stylesheet" href="css/add.css" type="text/css" media="screen" />
<link rel="stylesheet" href="utilLib/bootstrap.min.css" type="text/css" media="screen" />
<style type="text/css">
@media screen and (max-width:768px){
    .div_from_aoto{
        padding:6em 0 0 1em;
        width:100%;
        margin:100px auto;

    }
}
</style>
</head>
<body>
<div class="div_from_aoto" style="width: 300px;">
    <FORM action="logincheck.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
        <DIV class="control-group">
            <label class="laber_from">用户名</label>
            <DIV  class="controls" ><INPUT class="username" name="username" type=text placeholder=" 请输入用户名"><P class=help-block></P></DIV>
        </DIV>
        <DIV class="control-group">
            <LABEL class="laber_from">密码</LABEL>
            <DIV  class="controls" ><INPUT class="passwd" name="passwd" type=password placeholder=" 请输入密码">
            <P class=help-block></P></DIV>
        </DIV>

        <DIV class="control-group">
            <LABEL class="laber_from" ></LABEL>
            <DIV class="controls" ><button class="btn btn-success" style="width:120px;" >登录</button></DIV>
        </DIV>
  </FORM>
</div>
</body>
</html>

这里涉及到一个账号登陆问题,这里包括一个普通用户的登陆进入投票列表界面,还有一个是管理员进入后台管理系统(进入后台管理系统的代码见之后的文章)。账号密码的登陆,首先账号密码要匹配才能登陆,其次是防止通过url直接跳过账号密码的检测进入后台管理系统,解决方法是先对$_SESSION['user']'和$_SESSION['name']初始化为null,再对账号密码进行检测,然后再对管理员身份进行检测。具体代码如下:

<?php 
    include("../sqlsafe.php");
    include("../conn.php");
    $_SESSION['user'] = $_SESSION['admin'] = NULL;
    $username = $_POST['username'];
    $passwd = $_POST['passwd'];

    $sql = "select * from users where username='$username' and passwd='$passwd';";
    $result = $db->query($sql);
    if($result->num_rows > 0){
        $rank = $db->query("select * from users where username='$username' and admin='1'");
        @session_start();
        if($rank->num_rows > 0){
            $_SESSION['admin']=true;
            $_SESSION['user']=false;
            $_SESSION['name']=$username;
            echo "<meta http-equiv=\"Refresh\" content=\"0;url=../index.php\">";
        }else{
            $_SESSION['user']=true;
            $_SESSION['admin']=false;
            $_SESSION['name']=$username;
            echo "<meta http-equiv=\"Refresh\" content=\"0;url=../list.php\">";
        }

    }else{
        echo "<script>alert('账户或密码错误')</script>";
        echo "<meta http-equiv=\"Refresh\" content=\"0;url=login.html\">";
    }

?>

而防止通过url跳过检测的方法是通过session检测是否已经登陆过,若没有,则会跳转到登陆界面

<?php
    @session_start();
    if( !isset($_SESSION['admin']) || !isset($_SESSION['user']) || ( $_SESSION['user']!== true && $_SESSION['admin']!== true ) ){
        echo "<meta http-equiv=\"Refresh\" content=\"0;url=login.html\">";
        exit();
    }

其中conn.php的作用是链接数据库,具体代码为

<?php

    $db = new mysqli('127.0.0.1','root','lrwje520','simplevote1');
    $db->query("SET NAMES utf8");
    if(mysqli_connect_errno()){
        echo "据库连接失败";
        exit();
    }
?>

投票页面的呈现,这其中的难点就是多个投票标题的呈现,解决方法就是对数据库的提取,即$result_name = $db->query ( "select * from votename" );这是对整个表的提取,接下来是对表中每一行数据的提取这里用$row_name = mysqli_fetch_assoc ( $result_name ),当然 mysqli_fetch_assoc可以改成mysqli_fetch_array,作用是将以一个普通的关联数组的形式返回一条结果记录,用while语句对其进行判断,每次读取一行,直到读取完全部投票题目,就能实现把所有投票题目呈现出来,具体代码如下:

    <?php 
    include("conn.php");

    @session_start();
    header("Cache-control:private");
    if( $_GET['do'] ){
        if($_GET['do']=="logout"){
            unset($_SESSION['user']);
            unset($_SESSION['name']);
            @session_destroy();
        }
    }
    $result = $db->query("select * from sysconfig");
    $row = mysqli_fetch_assoc($result);
    $username = $_SESSION['name'];
    $result1 = $db->query("select * from users where username = '$username'");
    $row1 = mysqli_fetch_assoc($result1);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=2.0,width=device-width" /> 
<title></title>

<script type="text/javascript" src="admin/js/jquery.min.js"></script>
<link rel="stylesheet" href="main1.css" type="text/css" media="screen" />

</head>
<body>
<div class="main">
    <div id="header" >
        <div >
        <?php if( !isset($_SESSION['user']) || $_SESSION['user']!==true ){ ?>
            <a href="admin/login.html">登录投票</a>
        <?php }else{ ?>
            <span>你好,<?php echo $row1['name']; ?></span>
            <a href="admin/index.php">&nbsp;查看投票数据</a>
            <a href="admin/alter_password.html">&nbsp;修改密码</a>
            <a href="index.php?do=logout">&nbsp;登出</a>
        <?php } ?>
        </div>
    </div>
    <form action="index.php" method="get">
    <div class="content">
        <div>
            <h1><?php echo $row['vote_name']; ?></h1>
            <div class="description">
                <?php echo $row['description']; ?>
            </div>  
        </div>

        <?php
            $num = 0;
            $result_name = $db->query ( "select * from votename" );
            while ( $row_name = mysqli_fetch_assoc ( $result_name ) ) {
            $num += 1;
            $a = $row_name['question_name'];
        ?>
        <div class="mcontent">
        <a class="list-link" href="index.php?question_name=<?php echo $a ?>">
        <span  name='question_name' id="question_name">
        <h2><?php echo $num.".".$row_name['question_name']; ?></h2>
        </span>
        </a>
            </div>
            <?php } ?>
            <div style="clear:both;"></div>
        </div>
        <?php if($result_name->num_rows > 0){
        ?>
        <?php }else{ ?>
            <h1>当前没有投票</h1>
        <?php } ?>
        <br>
    </div>
  </form>


</div>
</body>
</html>

这当中if( !isset($_SESSION['user']) || $_SESSION['user']!==true ){ ?>
<a href="admin/login.html">登录投票</a>
的作用也是防止通过url跳过检测,当然也是使用了session来判断是否已经登陆,若还未登录则跳转到登陆界面。
当实现了对所有投票题目的呈现后,就要考虑每一个投票的具体呈现,这里涉及到每个投票内容与该投票题目的匹配问题,所以在上述的代码中,我将每一个投票题目设置了<a>标签,并且让其跳转页面的url设置href="index.php?question_name=<?php echo $a ?>"当中的$a就是该投票题目,这样一来在每个投票题目具体的呈现页面中我就可以通过解析该url来获取它的投票题目,进而获取该投票题目的全部内容,具体代码如下:

    <?php 
    include("conn.php");

    @session_start();
    header("Cache-control:private");
    if( $_GET['do'] ){
        if($_GET['do']=="logout"){
            unset($_SESSION['user']);
            unset($_SESSION['name']);
            @session_destroy();
        }
    }
    $result = $db->query("select * from sysconfig");
    $row = mysqli_fetch_assoc($result);
    ?>
<?php
    $url = $_SERVER["QUERY_STRING"];
    $test = parse_url("$url");
    $name = $test[path];
    $start = strrpos($name, '=');
    $unquestion_name = substr($name , $start+1);
    $question_name=urldecode($unquestion_name);
    $username = $_SESSION['name'];
    $result1 = $db->query("select * from users where username = '$username'");
    $row1 = mysqli_fetch_assoc($result1);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=2.0,width=device-width" /> 
<title></title>

<script type="text/javascript" src="admin/js/jquery.min.js"></script>
<link rel="stylesheet" href="main1.css" type="text/css" media="screen" />

</head>
<body>
<div class="main">
    <div id="header">
        <div >
        <?php if( !isset($_SESSION['user']) || $_SESSION['user']!==true ){ ?>
            <a href="admin/login.html">登录投票</a>
        <?php }else{ ?>
            <span>你好,<?php echo $row1['name']; ?></span>
            <a href="admin/index.php">&nbsp;查看投票数据</a>
            <a href="admin/alter_password.html">&nbsp;修改密码</a>
            <a href="index.php?do=logout">&nbsp;登出</a>
        <?php } ?>
        </div>
    </div>

    <form action="vote.php?question_name=<?php echo $question_name ?>" method="post">
    <div class="content">
        <div>
            <h1><?php echo $row['vote_name']; ?></h1>
            <div class="description">
                <?php echo $row['description']; ?>
            </div>
        </div> 

        <?php
            $result_cid = $db->query ( "select * from votename where question_name = '$question_name'" );
            $row_cid = mysqli_fetch_assoc ( $result_cid ) ;

        ?>
        <div class="mcontent">
            <h3><?php echo $question_name; ?></h3>
            <?php
                $result_option = $db->query ( "select * from voteoption where upid='" . $row_cid ['cid']. "';" );

                while ( $row_option = mysqli_fetch_assoc ( $result_option ) ) {
            ?>
            <div class="obox">
                <?php 
                    if($row_cid['votetype']=="0"){
                        echo '<input name="'.$row_cid['cid'].'" type="radio" value="'.$row_option['cid'].'">'.$row_option['optionname'];
                        ?>
                        <br>
                        <?php
                        $result_image = $db->query ( "select * from ccs_image where upid='".$row_option['cid']. "';" );
                        $row= mysqli_fetch_assoc ( $result_image);
                        if($row[filename]){
                            ?>
                            <h2><?php echo "$row[description] "?></h2>
                            <?php   
                            echo "<img src=\"admin/upload/lk/".$row[filename]."\"><br>";
                        }

                         ?>
                    <?php  }else if($row_cid['votetype']=="1"){
                        echo '<input name="'.$row_cid['cid'].'" type="checkbox" value="'.$row_option['cid'].'">'.$row_option['optionname'];
                    }
                ?>

            </div>
            <?php } ?>
            <div style="clear:both;"></div>
        </div>
        <?php if($result_cid->num_rows > 0){
        ?>


        <div class="votebu">
            <input  type="text" name="code_num" maxlength="4" />
            <img  onClick="this.src='img.php'" src="img.php"  alt="看不清,点击换一张">
            <input  name='question_name' id="question_name" type="submit" value="投票"  >
            <input name="num" type="hidden" value="<?php echo $num; ?>">
            <div ></div>
        </div>
        <?php }else{ ?>
            <h1>当前没有投票,请先登陆</h1>
        <?php } ?>
        <br>
    </div>
  </form>
  </div>
</body>
</html>

上述代码中获取url的方法为
$url = $_SERVER["QUERY_STRING"];//获取完成url
$test = parse_url("$url");//将url转变成数组
$name = $test[path];//提取url中path项,即question_name=$a
$start = strrpos($name, '=');
$unquestion_name = substr($name , $start+1);//获取乱码的question_name
$question_name=urldecode($unquestion_name);//解析乱码的question_name

在选项的呈现中包括一个是单选或者是多选的问题,在数据库中对单选或多选的题目设置了标签,votetype是0则为单选,1为多选所以有个判断语句判断但单选还是多选,而选项的呈现则和所有投票题目呈现的原理一样。
这里还包括对应照片的呈现,我的方式是将照片的信息存储在数据库中,而照片则存储在指定文件夹,通过寻找数据库中对应照片的名字到指定文件夹寻找该照片并呈现出来,输出照片的代码echo "<img src=\"admin/upload/lk/".$row[filename]."\"><br>"。

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页