php程式碼實現成績查詢的方法:1、建立前端登入頁面程式碼;2、通過「if (isset($_SESSION['username'])) {...}」語法實現判斷使用者是否登入;3、建立後端管理登入頁面;4、連線資料庫;5、通過「session_start(); if (isset($_COOKIE['username'])) {$_SESSION['']}」程式碼實現查詢成績即可。
php入門到就業線上直播課:進入學習
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API偵錯工具:
本教學操作環境:Windows7系統、PHP8.1版、Dell G3電腦。
php程式碼怎麼實現成績查詢?
PHP成績查詢系統
一個非常簡陋的PHP成績查詢系統,期末作業。
因為上課打醬油了,所以這也是最後幾天搗鼓出來的,程式碼都是東拼西湊的,只有簡單的增刪改查功能。就醬紫。
資料庫:
一共這麼幾個檔案(html、css、php都寫一塊了)
然後介面:(就長這樣)
程式碼是按上圖的檔案順序排的
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0px;
padding: auto;
}
fieldset {
margin: auto;
margin-top: 200px;
width: 400px;
text-align: center;
}
ul li {
margin: 0;
padding: 0;
}
form {
margin: 40px 30px 0;
}
form li {
list-style: none;
padding: 5px 0;
}
.login_btn {
border: none;
background: #01A4F1;
color: #fff;
font-size: 14px;
font-weight: bold;
height: 28px;
line-height: 28px;
padding: 0 10px;
cursor: pointer;
}
a:link {
text-decoration: none;
color: blue;
}
a:visited {
color: blue;
text-decoration: none;
}
.return_but {
float: right;
}
</style>
</head>
<body>
<form action="#" method="POST">
<fieldset>
<legend>新增學生成績</legend>
<ul>
<li>
請輸入學生的<b>成績</b>
<span><a href="show_teacher.php">返回</a></span>
</li>
<li>
學號:
<input type="text" name="username" />
</li>
<li>
語文:
<input type="text" name="yuwen" />
</li>
<li>
數學:
<input type="text" name="shuxue" />
</li>
<li>
英語:
<input type="text" name="yingyu" />
</li>
<li>
綜合:
<input type="text" name="zonghe" />
</li>
<li>
<input type="submit" name="add_score" value="確認新增" />
</li>
</ul>
</fieldset>
</form>
<?php
header("Content-Type:text/html; charset=utf-8");
session_start();
include("conn.php");
$con = mysqli_connect("localhost:3306", "root", "", "resultsquerysystem");
mysqli_query($con, "set names utf8");
if (isset($_SESSION['username'])) {
if (isset($_POST['add_score'])) {
$username = $_POST['username'];
$yuwen = $_POST['yuwen'];
$shuxue = $_POST['shuxue'];
$yingyu = $_POST['yingyu'];
$zonghe = $_POST['zonghe'];
$sql = "insert into score (username,語文,數學,英語,綜合) values('$username','$yuwen','$shuxue','$yingyu','$zonghe')";
mysqli_query($con, $sql);
if (mysqli_affected_rows($con) > 0) {
echo "<script> alert('新增成功'); location.href='show_teacher.php';</script>";
}
}
} else {
//快取意外被清除後、
echo "使用者資訊丟失,3秒後返回登陸介面";
header('refresh: 3; url=index.php');
}
?>
</body>
</html>
登入後複製
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0px;
padding: auto;
}
fieldset {
margin: auto;
margin-top: 200px;
width: 400px;
text-align: center;
}
ul li {
margin: 0;
padding: 0;
}
form {
margin: 40px 30px 0;
}
form li {
list-style: none;
padding: 5px 0;
}
.login_btn {
border: none;
background: #01A4F1;
color: #fff;
font-size: 14px;
font-weight: bold;
height: 28px;
line-height: 28px;
padding: 0 10px;
cursor: pointer;
}
a:link {
text-decoration: none;
color: blue;
}
a:visited {
color: blue;
text-decoration: none;
}
.return_but {
float: right;
}
</style>
</head>
<body>
<form action="#" method="POST">
<fieldset>
<legend>新增學生資訊</legend>
<ul>
<li>
請輸入需要新增學生的<b>學號</b>和<b>登陸密碼</b>
<span><a href="show_teacher.php">返回</a></span>
</li>
<li>
學號:
<input type="text" name="username" />
</li>
<li>
密碼:
<input type="password" name="password" />
</li>
<li>
<input type="submit" name="add_student" value="確認新增" />
</li>
</ul>
</fieldset>
</form>
<?php
header("Content-Type:text/html; charset=utf-8");
session_start();
include("conn.php");
$con = mysqli_connect("localhost:3306", "root", "", "resultsquerysystem");
mysqli_query($con, "set names utf8");
if (isset($_SESSION['username'])) {
if (isset($_POST['add_student'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "insert into student (username,password) values('$username','$password')";
mysqli_query($con, $sql);
if (mysqli_affected_rows($con) > 0) {
echo "<script> alert('新增成功'); location.href='show_teacher.php';</script>";
}
}
} else {
//快取意外被清除後、
echo "使用者資訊丟失,3秒後返回登陸介面";
header('refresh: 3; url=index.php');
}
?>
</body>
</html>
登入後複製
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0px;
padding: auto;
}
fieldset {
margin: auto;
margin-top: 200px;
width: 400px;
text-align: center;
}
ul li {
margin: 0;
padding: 0;
}
form {
margin: 40px 30px 0;
}
form li {
list-style: none;
padding: 5px 0;
}
.login_btn {
border: none;
background: #01A4F1;
color: #fff;
font-size: 14px;
font-weight: bold;
height: 28px;
line-height: 28px;
padding: 0 10px;
cursor: pointer;
}
a:link {
text-decoration: none;
color: blue;
}
a:visited {
color: blue;
text-decoration: none;
}
.return_but {
float: right;
}
</style>
</head>
<body>
<form action="#" method="POST">
<fieldset>
<legend>修改學生成績</legend>
<ul>
<li>
學號:<?php echo $_GET['username'] ?>
<span><a href="show_teacher.php">返回</a></span>
</li>
<li>
語文:
<input type="text" name="yuwen" value="<?php echo $_GET['yuwen'] ?>" />
</li>
<li>
數學:
<input type="text" name="shuxue" value="<?php echo $_GET['shuxue'] ?>" />
</li>
<li>
英語:
<input type="text" name="yingyu" value="<?php echo $_GET['yingyu'] ?>" />
</li>
<li>
綜合:
<input type="text" name="zonghe" value="<?php echo $_GET['zonghe'] ?>" />
</li>
<li>
<input type="submit" name="alter_score" value="確認修改" />
</li>
</ul>
</fieldset>
</form>
<?php
header("Content-Type:text/html; charset=utf-8");
session_start();
include("conn.php");
$con = mysqli_connect("localhost:3306", "root", "", "resultsquerysystem");
mysqli_query($con, "set names utf8");
if (isset($_SESSION['username'])) {
if (isset($_POST['alter_score'])) {
$username = $_GET['username'];
$yuwen = $_POST['yuwen'];
$shuxue = $_POST['shuxue'];
$yingyu = $_POST['yingyu'];
$zonghe = $_POST['zonghe'];
$sql = "UPDATE `score` SET`語文`=$yuwen,`數學`=$shuxue,`英語`=$yingyu,`綜合`=$zonghe WHERE username = $username";
mysqli_query($con, $sql);
if (mysqli_affected_rows($con) > 0) {
echo "<script> alert('修改成功'); location.href='show_teacher.php';</script>";
}
}
} else {
//快取意外被清除後、
echo "使用者資訊丟失,3秒後返回登陸介面";
header('refresh: 3; url=index.php');
}
?>
</body>
</html>
登入後複製
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<?php
$con =mysqli_connect("localhost:3306","root","","miniblog");
if(!$con){
die("連結錯誤");
}
mysqli_query($con,"set names utf8");
?>
</body>
</html>
登入後複製
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<?php
header("Content-Type:text/html; charset=utf-8");
session_start();
include("conn.php");
$con = mysqli_connect("localhost:3306", "root", "", "resultsquerysystem");
mysqli_query($con, "set names utf8");
if (isset($_SESSION['username'])) {
if (isset($_GET['id'])) {
$username = $_GET['id'];
echo $username;
$sql = "delete from score where username = $username";
mysqli_query($con, $sql);
if (mysqli_affected_rows($con) > 0) {
echo "<script> alert('刪除成功'); location.href='show_teacher.php';</script>";
}
}
} else {
//快取意外被清除後、
echo "使用者資訊丟失,3秒後返回登陸介面";
header('refresh: 3; url=index.php');
}
?>
</body>
</html>
登入後複製
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
* {
margin: 0px;
padding: auto;
}
fieldset {
margin: auto;
margin-top: 200px;
width: 400px;
text-align: center;
}
ul li {
margin: 0;
padding: 0;
}
form {
margin: 40px 30px 0;
}
form li {
list-style: none;
padding: 5px 0;
}
.login_btn {
border: none;
background: #01A4F1;
color: #fff;
font-size: 14px;
font-weight: bold;
height: 28px;
line-height: 28px;
padding: 0 10px;
cursor: pointer;
}
</style>
</head>
<body>
<form action="#" method="POST">
<fieldset>
<legend>使用者登入</legend>
<ul>
<li>
使用者名稱:
<input type="text" name="username" />
</li>
<li>
密 碼:
<input type="password" name="password" />
</li>
<li>
<input type="submit" name="login_student" value="學生登入" />
<input type="submit" name="login_teacher" value="教師登入" />
</li>
</ul>
<?php
header("Content-Type:text/html;charset=utf-8");
include("conn.php");
$con = mysqli_connect("localhost:3306", "root", "", "resultsquerysystem");
session_start();
//點選學生登陸按鈕
if (isset($_POST['login_student'])) {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
if (($username == '') || ($password == '')) {
header('refresh: 3; url=index.php');
echo "該使用者名稱或者密碼不能為空,3秒後跳轉到登入頁面";
exit;
} else {
$sql = "select * from student where username='$username'";
$res = mysqli_query($con, $sql);
$n = mysqli_num_rows($res);
if ($n > 0) {
$row = mysqli_fetch_assoc($res);
$pwd = $row['password'];
//使用者名稱或密碼錯誤
if ($password != $pwd) {
# code...
header('refresh: 3; url=index.php');
echo "使用者名稱或者密碼錯誤,3秒後跳轉到登入頁面";
} else {
//登入成功,將使用者資訊儲存到session中
$_SESSION['username'] = $username;
$_SESSION['islogin'] = 1;
//使用者資訊儲存到Cookie ,1天
setcookie("username", $username, time() + 24 * 60 * 60);
setcookie(
"pw",
md5($username . md5($password)),
time() + 24 * 60 * 60
);
//跳轉到顯示頁面
header("location:show_student.php");
}
} else {
header('refresh: 3; url=index.php');
echo "使用者名稱或者密碼錯誤,3秒後跳轉到登入頁面";
}
}
}
//點選教師登入按鈕
elseif (isset($_POST['login_teacher'])) {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
if (($username == '') || ($password == '')) {
header('refresh: 3; url=index.php');
echo "該使用者名稱或者密碼不能為空,3秒後跳轉到登入頁面";
exit;
} else {
$sql = "select * from teacher where username='$username'";
$res = mysqli_query($con, $sql);
$n = mysqli_num_rows($res);
if ($n > 0) {
$row = mysqli_fetch_assoc($res);
$pwd = $row['password'];
//使用者名稱或密碼錯誤
if ($password != $pwd) {
# code...
header('refresh: 3; url=index.php');
echo "使用者名稱或者密碼錯誤,3秒後跳轉到登入頁面";
} else {
//登入成功,將使用者資訊儲存到session中
$_SESSION['username'] = $username;
$_SESSION['islogin'] = 1;
//使用者資訊儲存到Cookie ,1天
setcookie("username", $username, time() + 24 * 60 * 60);
setcookie(
"pw",
md5($username . md5($password)),
time() + 24 * 60 * 60
);
//跳轉到顯示頁面
header("location:show_teacher.php");
}
} else {
header('refresh: 3; url=index.php');
echo "使用者名稱或者密碼錯誤,3秒後跳轉到登入頁面";
}
}
}
?>
</fieldset>
</form>
</body>
</html>
登入後複製
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
a:link {
text-decoration: none;
color: blue;
}
a:visited {
color: blue;
text-decoration: none;
}
</style>
</head>
<body>
<?php
header("Content-Type:text/html;charset=utf-8");
session_start();
//清除session
$username = $_SESSION['username'];
$_SESSION = array();
session_destroy();
//清除cookie
setcookie("username", '', time() - 1);
setcookie("code", '', time() - 1);
echo "<a href='index.php'>點選重新登入</a>";
header('refresh: 5; url=index.php');
echo "<br />5秒鐘後自動返回到主頁";
?>
</body>
</html>
登入後複製
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0px;
padding: auto;
}
a:link {
text-decoration: none;
color: blue;
}
a:visited {
color: blue;
text-decoration: none;
}
#box {
margin: auto;
margin-top: 200px;
width: 800px;
text-align: center;
}
table {
width: 700px;
padding: 0;
margin: 0 auto;
}
td {
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
font-size: 11px;
padding: 6px 6px 6px 12px;
color: #4f6b72;
}
tr:hover {
background-color: #B0C4DE;
}
th {
font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #4f6b72;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
letter-spacing: 2px;
text-transform: uppercase;
text-align: center;
padding: 6px 6px 6px 12px;
background: #CAE8EA no-repeat;
}
</style>
</head>
<body>
<div id="box">
<?php
header("Content-Type:text/html;charset=utf-8");
session_start();
//首先判斷Cookie是否有記住使用者資訊
if (isset($_COOKIE['username'])) {
$_SESSION['username'] = $_COOKIE['username'];
$_SESSION['islogin'] = 1;
}
if (isset($_SESSION['islogin'])) {
//已經登入
echo "成績查詢中心!<br/><br/>你的學號:" . $_SESSION['username'] . " ";
echo "<a href='logout.php'>登出</a>";
} else { //為登入
echo "你還未登入,請<a href='index.php'>登入</a>";
}
?>
<table>
<thead>
<th>語文</th>
<th>數學</th>
<th>英語</th>
<th>綜合</th>
</thead>
<?php
$db = new mysqli("localhost", "root", "", "resultsquerysystem");
$sql = "select * from score where username = '" . $_SESSION['username'] . "'";
$r = $db->query($sql);
//傳值
while ($attr = $r->fetch_row()) {
echo " <tr>
<td>{$attr[1]}</td>
<td>{$attr[2]}</td>
<td>{$attr[3]}</td>
<td>{$attr[4]}</td>
</tr>";
}
?>
</table>
</div>
</body>
</html>
登入後複製
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
a:link {
text-decoration: none;
color: blue;
}
a:visited {
color: blue;
text-decoration: none;
}
* {
margin: 0px;
padding: auto;
}
#box {
margin: auto;
margin-top: 200px;
width: 800px;
text-align: center;
}
table {
width: 700px;
padding: 0;
margin: 20px auto;
}
td {
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
font-size: 11px;
padding: 6px 6px 6px 12px;
color: #4f6b72;
}
tr:hover {
background-color: #B0C4DE;
}
th {
font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #4f6b72;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
letter-spacing: 2px;
text-transform: uppercase;
text-align: center;
padding: 6px 6px 6px 12px;
background: #CAE8EA no-repeat;
}
.login_btn {
border: none;
background: #01A4F1;
color: #fff;
font-size: 14px;
font-weight: bold;
height: 28px;
line-height: 28px;
padding: 0 10px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="box">
<?php
header("Content-Type:text/html;charset=utf-8");
session_start();
//首先判斷Cookie是否有記住使用者資訊
if (isset($_COOKIE['username'])) {
$_SESSION['username'] = $_COOKIE['username'];
$_SESSION['islogin'] = 1;
}
if (isset($_SESSION['islogin'])) {
//已經登入
echo "成績查詢中心!<br/><br/>工號:" . $_SESSION['username'] . " ";
echo "<a href='logout.php'>登出</a>";
} else { //為登入
echo "你還未登入,請<a href='index.php'>登入</a>";
}
?>
<form method="post" action="delete_score.php">
<table>
<thead>
<th>學號</th>
<th>語文</th>
<th>數學</th>
<th>英語</th>
<th>綜合</th>
<th>操作</th>
</thead>
<?php
$db = new mysqli("localhost", "root", "", "resultsquerysystem");
$sql = "select * from score";
$r = $db->query($sql);
//傳值
while ($attr = $r->fetch_row()) {
echo " <tr>
<td>{$attr[0]}</td>
<td>{$attr[1]}</td>
<td>{$attr[2]}</td>
<td>{$attr[3]}</td>
<td>{$attr[4]}</td>
<td>
<a href='alter_score.php?username=$attr[0]&yuwen=$attr[1]&shuxue=$attr[2]&yingyu=$attr[3]&zonghe=$attr[4]'>修改</a>
<a href='delete_score.php?id=$attr[0]'>刪除</a>
</td>
</tr>";
}
?>
</table>
</form>
<a href="add_student.php"><button>新增學生資訊</button></a>
<a href="add_score.php"><button>新增學生成績</button></a>
</div>
</body>
</html>
登入後複製
推薦學習:《》
以上就是php程式碼怎麼實現成績查詢的詳細內容,更多請關注TW511.COM其它相關文章!