开发PHP小项目-查询录取结果(2):后端设计
数据库设计
新建数据库,库名search,字符集为utf8 – UTF-8 Unicode
新建表student,四个字段,一个num(准考证)一个name(学生姓名)一个sdept(院系) 一个profession(专业)
新建表admin,管理员信息表,id设置为自动递增,默认为1。
路由设计
路由选择用文件包含,通过GET传进参数实现跳转页面。在准考文件夹新建一个index.php文件作为路由。
这里用到一个issst()函数,用于检测变量是否已设置并且非空。返回值为true和false
<?php
if(isset($_GET['a'])){
$router=$_GET['a'];
}else{
$router='search';//默认页面为search.php
}
switch ($router) {
case 'search':
include('search.php');
break;
case 'login':
include('login.php');
break;
case 'upload':
include('upload.php');
break;
case 'update':
include('update.php');
break;
case '':
include('insert.php');
break;
}
?>
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #### 登录验证设计
在login.php的基础上改写。主要是将登录成功的用户名存入session中,以便保存登录状态。
```php if($data->num_rows){ session_start(); $_SESSION['account']=$account;//将用户名存入session echo '<script>alert("登陆成功");window.location.href="index.php?a=upload";</script>'; }else{ echo ( "<script >alert('账号或密码错误')</script>"); } }
|
1 2 3 4 5 6 7 8 9
| if(isset($_POST['account'])) $account=trim($_POST['account']); $passwd = trim($_POST['passwd']); $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn, "utf8"); $sql = "SELECT * FROM admin WHERE account='$account' and passwd='$passwd'"; $data = $conn->query($sql);
|
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| <?php $servername = "127.0.0.1"; $username = "root"; $password = "root"; $dbname = "search"; ?>
<?php if(isset($_POST['account'])){ $account=trim($_POST['account']); $passwd = trim($_POST['passwd']); $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn, "utf8"); $sql = "SELECT * FROM admin WHERE account='$account' and passwd='$passwd'"; $data = $conn->query($sql); if($data->num_rows){ session_start(); $_SESSION['account']=$account; echo '<script>alert("登陆成功");window.location.href="index.php?a=upload";</script>'; }else{ echo ( "<script >alert('账号或密码错误')</script>"); } } ?>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>管理员登录</title> <style> body{ background-image: url(../images/bg.jpg); background-position: center; background-size: cover; min-height: 100vh; } .button{ color: background-color: border-radius: 1px; border:1px solid width: 50px; height: 25px;
} .input{
border-radius: 1px; border:1px solid width:410px; height: 25px;
} .box{
filter: alpha(Opacity=70); -moz-opacity:0.7; opacity: 0.7; border-radius: 1px solid box-shadow: 1px 1px 50px width:410px; height: 50px; margin: auto; margin-top: 180px; margin-right: center; border-radius: 6px; } .boxtittle{ width: 310px; margin: auto; margin-right: center; padding-top: 10px; text-align:center; cloor:
} .inputbox{
width: 410px; margin: auto; padding-top: 10px; } </style> </head> <body> <div class="box"> <div class="boxtitle"> <p><h3>管理员登陆</h3></p> </div > <div class="inputbox"> <form action="login.php" method="post"> <input class='inputbox' type="text" name="account" placeholder="请输入您的管理员账号"> <input class='inputbox' type='password' name='passwd' placeholder='请输入您的密码'> <input class='button' type="submit" value="登录" > <input type="button" value="返回主页" onclick="window.location.href='search.php';"/> </form> </div> </body> </html>
|
保存上传文件和设置路径
在upload.php写好的表单下面加这串代码就可以实现简单的上传文件,还没做好过滤等工作。(文件保存在zhunkao文件的uploads文件夹下)
1 2 3 4
| <?php
move_uploaded_file($_FILES["upfile"]["tmp_name"], "./uploads/".$_FILES["upfile"]["name"]); ?>
|
保存了文件之后,进行文件解析,将文件的数据保存到数据库中。这里用到PHPExcel,将PHPExcel文件夹和PHPExcel.php保存在同一目录下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| $sFileName="uploads/".$filename; require_once 'PHPExcel.php'; require_once 'PHPExcel/IOFactory.php'; require_once 'PHPExcel/Reader/Excel5.php';
$objReader = PHPExcel_IOFactory::createReader('Excel5'); $objPHPExcel = $objReader->load($sFileName); $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); $k = 0;
$conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn, "utf8"); for($j=2;$j<=$highestRow;$j++) { $a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue(); $b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue(); $c = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue(); $d = $objPHPExcel->getActiveSheet()->getCell("D".$j)->getValue(); $sql = "INSERT INTO student (num,name,sdept,profession) VALUES ('$a','$b','$c','$d')"; $result = mysqli_query($conn, $sql); if($result){ $sta="ok"; } else{ $sta="no"; } }
|
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>上传信息</title> <style> body{ background-image: url(../images/bg.jpg); background-position: center; background-size: cover; min-height: 100vh; } .button{ color: background-color: border-radius: 1px; border:1px solid width: 50px; height: 25px;
} .input{
border-radius: 1px; border:1px solid width:410px; height: 25px;
} .box{
filter: alpha(Opacity=70); -moz-opacity:0.7; opacity: 0.7; border-radius: 1px solid box-shadow: 1px 1px 50px width:410px; height: 50px; margin: auto; margin-top: 180px; margin-right: center; border-radius: 6px; } .boxtittle{ width: 310px; margin: auto; margin-right: center; padding-top: 10px; text-align:center; cloor:
} .inputbox{
width: 410px; margin: auto; padding-top: 10px; } </style> </head> <body> <div class="box"> <div class="boxtitle"> <p><h3>上传信息</h3></p> </div > <div class="inputbox"> <form action="upload.php" method="post" enctype="multipart/form-data"> <input class='input' type="file" name="upfile" > <input class='botton' type="submit" value="提交"> </br></br> <input type="button" value="插入信息" onclick="window.location.href='insert.php';"/> <input type="button" value="修改信息" onclick="window.location.href='update.php';"/> <input type="button" value="上一页" onclick="window.location.href='search.php';"/>
</form> </div> </body> </html>
<?php $servername = "127.0.0.1"; $username = "root"; $password = "root"; $dbname = "search"; ?>
<?php error_reporting(0); session_start(); if(!isset($_SESSION['account'])){ die("<script >alert('请登录');window.location.href=\"index.php?a=login\"; </script>"); } $filename=@$_FILES["upfile"]["name"]; move_uploaded_file(@$_FILES["upfile"]["tmp_name"], "./uploads/".@$_FILES["upfile"]["name"]); $sFileName="uploads/".$filename; require_once 'PHPExcel.php'; require_once 'PHPExcel/IOFactory.php'; require_once 'PHPExcel/Reader/Excel5.php';
$objReader = PHPExcel_IOFactory::createReader('Excel5'); $objPHPExcel = $objReader->load($sFileName); $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); $k = 0;
$conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn, "utf8"); for($j=2;$j<=$highestRow;$j++) { $a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue(); $b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue(); $c = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue(); $d = $objPHPExcel->getActiveSheet()->getCell("D".$j)->getValue(); $sql = "INSERT INTO student (num,name,sdept,profession) VALUES ('$a','$b','$c','$d')"; $result = mysqli_query($conn, $sql); if($result){ $sta="ok"; } else{ $sta="no"; } } $highestRow=$highestRow-1; if($sta=="ok"){ echo '<script>alert("数据导入成功!");window.location.href="upload.php";</script>'; } ?>
|
页面演示:
成功导入数据库的student表里
插入信息页面后端设计
在insert.php的基础上,加入登陆验证,连接数据库操作。
连接数据库:
1 2 3 4 5 6
| $servername = "127.0.0.1"; $username = "root"; $password = "root"; $dbname = "search"; $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn, "utf8");
|
数据库操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| if(isset($_POST['num'])){ $num=$_POST['num']; $name = $_POST['name']; $sdept=$_POST['sdept']; $profession=($_POST['profession']); $sql = "INSERT INTO student (num,name,sdept,profession) VALUES ('$num','$name','$sdept','$profession')"; $result = mysqli_query($conn, $sql); if($result){ $sta="ok"; } else{ $sta="no"; } if($sta=="ok"){ echo '<script>alert("数据插入成功!");window.location.href="insert.php";</script>'; } }
|
源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>上传信息</title> <style> body{ background-image: url(../images/bg.jpg); background-position: center; background-size: cover; min-height: 100vh; } .button{ color: background-color: border-radius: 1px; border:1px solid width: 50px; height: 25px;
} .input{
border-radius: 1px; border:1px solid width:410px; height: 25px;
} .box{
filter: alpha(Opacity=70); -moz-opacity:0.7; opacity: 0.7; border-radius: 1px solid box-shadow: 1px 1px 50px width:410px; height: 50px; margin: auto; margin-top: 180px; margin-right: center; border-radius: 6px; } .boxtittle{ width: 310px; margin: auto; margin-right: center; padding-top: 10px; text-align:center; cloor:
} .inputbox{
width: 410px; margin: auto; padding-top: 10px; } </style> </head> <body> <div class="box"> <div class="boxtitle"> <p><h3>插入信息</h3></p> </div > <div class="inputbox"> <form action="insert.php" method="post" enctype="multipart/form-data"> <input class='input' type="test" name="num" placeholder='请输入插入学生的准考证号'> <input class='input' type="test" name="name" placeholder='请输入插入学生的姓名'> <input class='input' type="test" name="sdept" placeholder='请输入插入学生的院系'> <input class='input' type="test" name="profession" placeholder='请输入插入学生的专业'> <input class='botton' type="submit" value="插入"> <input type="button" value="上一页" onclick="window.location.href='upload.php';"/>
</form> </div> </body> </html>
<?php $servername = "127.0.0.1"; $username = "root"; $password = "root"; $dbname = "search"; $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn, "utf8"); session_start(); if(!isset($_SESSION['account'])){ die("<script >alert('请登录');window.location.href=\"index.php?a=login\"; </script>"); } if(isset($_POST['num'])){ $num=$_POST['num']; $name = $_POST['name']; $sdept=$_POST['sdept']; $profession=($_POST['profession']); $sql = "INSERT INTO student (num,name,sdept,profession) VALUES ('$num','$name','$sdept','$profession')"; $result = mysqli_query($conn, $sql); if($result){ $sta="ok"; } else{ $sta="no"; } if($sta=="ok"){ echo '<script>alert("数据插入成功!");window.location.href="insert.php";</script>'; } } ?>
|
修改信息页面后端设计
与插入信息页面相似,不多做解释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>上传信息</title> <style> body{ background-image: url(../images/bg.jpg); background-position: center; background-size: cover; min-height: 100vh; } .button{ color: background-color: border-radius: 1px; border:1px solid width: 50px; height: 25px;
} .input{
border-radius: 1px; border:1px solid width:410px; height: 25px;
} .box{
filter: alpha(Opacity=70); -moz-opacity:0.7; opacity: 0.7; border-radius: 1px solid box-shadow: 1px 1px 50px width:410px; height: 50px; margin: auto; margin-top: 180px; margin-right: center; border-radius: 6px; } .boxtittle{ width: 310px; margin: auto; margin-right: center; padding-top: 10px; text-align:center; cloor:
} .inputbox{
width: 410px; margin: auto; padding-top: 10px; } </style> </head> <body> <div class="box"> <div class="boxtitle"> <p><h3>修改信息</h3></p> </div > <div class="inputbox"> <form action="alter.php" method="post" enctype="multipart/form-data"> <input class='input' type="test" name="num" placeholder='请输入要修改学生的准考证号'> <input class='input' type="test" name="name" placeholder='请输入修改后姓名'> <input class='input' type="test" name="sdept" placeholder='请输入修改后的院系'> <input class='input' type="test" name="profession" placeholder='请输入修改后的专业'> <input class='botton' type="submit" value="修改"> <input type="button" value="上一页" onclick="window.location.href='upload.php';"/>
</form> </div> </body> </html>
<?php $servername = "127.0.0.1"; $username = "root"; $password = "root"; $dbname = "search"; $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn, "utf8"); session_start(); if(!isset($_SESSION['account'])){ die("<script >alert('请登录');window.location.href=\"index.php?a=login\"; </script>"); } if(isset($_POST['num'])){ $num=$_POST['num']; $name = $_POST['name']; $sdept=$_POST['sdept']; $profession=($_POST['profession']); $sql= "update student set name='$name',sdept='$sdept',profession='$profession' where num='$num';"; $result = mysqli_query($conn, $sql); if($result){ $sta="ok"; } else{ $sta="no"; } if($sta=="ok"){ echo '<script>alert("数据修改成功!");window.location.href="alter.php";</script>'; } } ?>
|
查询页面后端设计
查询数据库数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?php $servername = "127.0.0.1"; $username = "root"; $password = "root"; $dbname = "search"; $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn, "utf8");
if(isset($_POST['num'])){ $num=trim($_POST['num']); $sql = "SELECT * FROM student WHERE num='$num' "; $data= $conn->query($sql); $content = $data->fetch_array(MYSQL_ASSOC);
}
?>
|
源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| <?php $servername = "127.0.0.1"; $username = "root"; $password = "root"; $dbname = "search"; $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn, "utf8");
if(isset($_POST['num'])){ $num=trim($_POST['num']); $sql = "SELECT * FROM student WHERE num='$num' "; $data= $conn->query($sql); $content = $data->fetch_array(MYSQL_ASSOC);
}
?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>录取查询</title> <style> body{ background-image: url(../images/bg.jpg); background-position: center; background-size: cover; min-height: 100vh; } .button{ color: background-color: border-radius: 1px; border:1px solid width: 50px; height: 25px;
} .input{
border-radius: 1px; border:1px solid width:410px; height: 25px;
} .box{
filter: alpha(Opacity=70); -moz-opacity:0.7; opacity: 0.7; border-radius: 1px solid box-shadow: 1px 1px 50px width:410px; height: 50px; margin: auto; margin-top: 180px; margin-right: center; border-radius: 6px; } .boxtittle{ width: 310px; margin: auto; margin-right: center; padding-top: 10px; text-align:center; cloor:
} .inputbox{
width: 410px; margin: auto; padding-top: 10px; } .messgbox{ filter:alpha(Opacity=80); -moz-opacity:0.8; opacity: 0.8; border-radius: 1px solid box-shadow:1px 1px 50px width: 400px; height:200px; margin:auto; margin-top: 20px; border-radius: 6px; background-color: } .adbox{ width: 75px; } </style>
</head> <body> <div> <input class='adbox' type='botton' value="管理员登陆" onclick="window.location.href='login.php';"> </div> <div class="box"> <div class="boxtitle"> <p><h3>录取查询</h3></p> </div > <div class="inputbox"> <form action="search.php" method="post"> <input class='inputbox' type="text" name="num" placeholder="请输入您的准考证号"> <input class='button' type="submit" value="查询" > </form> <div class="messgbox"> <p><h3><strong><?php if(@$data->num_rows) { echo ("你已被广州交通大学录取!<br>你的准考证号是:".$content["num"]); echo ("<br>姓名:".$content["name"]); echo ("<br>院系:".$content["sdept"]); echo ("<br>专业:".$content["profession"]); } else{echo "请输入正确的准考证号";}
?></strong></h3></p>
</div>
</body> </html>
|
登录页面后端设计
数据库相关操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <?php $servername = "127.0.0.1"; $username = "root"; $password = "root"; $dbname = "search"; ?>
<?php if(isset($_POST['account'])){ $account=trim($_POST['account']); $passwd = trim($_POST['passwd']); $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn, "utf8"); $sql = "SELECT * FROM admin WHERE account='$account' and passwd='$passwd'"; $data = $conn->query($sql); if($data->num_rows){ session_start(); $_SESSION['account']=$account; echo '<script>alert("登陆成功");window.location.href="index.php?a=upload";</script>'; }else{ echo ( "<script >alert('账号或密码错误')</script>"); } } ?>
|
其中,将登录信息存入session,以便登陆验证
1 2 3 4 5
| session_start(); $_SESSION['account']=$account; echo '<script>alert("登陆成功");window.location.href="index.php?a=upload";</script>'; }else{ echo ( "<script >alert('账号或密码错误')</script>");
|
源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| <?php $servername = "127.0.0.1"; $username = "root"; $password = "root"; $dbname = "search"; ?>
<?php if(isset($_POST['account'])){ $account=trim($_POST['account']); $passwd = trim($_POST['passwd']); $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn, "utf8"); $sql = "SELECT * FROM admin WHERE account='$account' and passwd='$passwd'"; $data = $conn->query($sql); if($data->num_rows){ session_start(); $_SESSION['account']=$account; echo '<script>alert("登陆成功");window.location.href="index.php?a=upload";</script>'; }else{ echo ( "<script >alert('账号或密码错误')</script>"); } } ?>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>管理员登录</title> <style> body{ background-image: url(../images/bg.jpg); background-position: center; background-size: cover; min-height: 100vh; } .button{ color: background-color: border-radius: 1px; border:1px solid width: 50px; height: 25px;
} .input{
border-radius: 1px; border:1px solid width:410px; height: 25px;
} .box{
filter: alpha(Opacity=70); -moz-opacity:0.7; opacity: 0.7; border-radius: 1px solid box-shadow: 1px 1px 50px width:410px; height: 50px; margin: auto; margin-top: 180px; margin-right: center; border-radius: 6px; } .boxtittle{ width: 310px; margin: auto; margin-right: center; padding-top: 10px; text-align:center; cloor:
} .inputbox{
width: 410px; margin: auto; padding-top: 10px; } </style> </head> <body> <div class="box"> <div class="boxtitle"> <p><h3>管理员登陆</h3></p> </div > <div class="inputbox"> <form action="login.php" method="post"> <input class='inputbox' type="text" name="account" placeholder="请输入您的管理员账号"> <input class='inputbox' type='password' name='passwd' placeholder='请输入您的密码'> <input class='button' type="submit" value="登录" > <input type="button" value="返回主页" onclick="window.location.href='search.php';"/> </form> </div> </body> </html>
|